0

Date Math in Microsoft Dynamics CRM

by BenVollmer 28. May 2010 21:08

image In case you haven’t noticed, handling dates in JScript is less than ideal. In the book I keep here at my side, there is easily two chapters on handling dates in JScript. (Mind you, not CRM, just regular old JScript.) And then throw doing some math on dates, and it hates you. And you can burn some time in trying to troubleshoot everything.

For a demo, we wanted to take a start date, add a number of days to it and then automatically create the end date. And several hours later, I called my “LifelineChet Kloss from Michigan. In about 10 minutes Chet had my code fixed up.

It turns out in the last Variable, I failed to tell JScript this was a date, which threw it in a bit of a tizzy. So Chet fixed that for me and low and behold, it actually works.

The trick for me at least was converting the number of days into Milliseconds, which is the least common denominator in JScript.

So in the OnChange of Number of Days this was put in the OnChange

var Days = parseInt(crmForm.all.new_numberofdays.DataValue);
var Start = crmForm.all.new_startdate.DataValue;
var Hours = Days * 24;
var Minutes = Hours * 60;
var Seconds = Minutes * 60;
var Milliseconds = Seconds * 1000;
var Finish =  new Date(Start.getTime() + Milliseconds);
crmForm.all.new_enddate.DataValue =Finish;

In the OnChange of Start Date the following Code was placed

crmForm.all.new_numberofdays.FireOnChange();

And in the OnSave Event for the Form, the following code was used:

crmForm.all.new_enddate.ForceSubmit = true;

Tags: ,

Demo Tools

1

Update: Inserting City and State Automatically into Microsoft Dynamics CRM

by BenVollmer 7. May 2010 00:21

It is really hard to believe that I have been blogging for this long. And it is funny how things change. And some things stay the same. So about 4 years ago, I wrote a blog article on using Yahoo’s Web Services to get the City and State based upon a Zip Code Entry.

In talking with one of my co-workers about a demo she was doing, I recommended she look at it as a way to show how we can interface with external systems. Well guess what, Yahoo changed their API’s all around.

So now you need to get an application key for Yahoo and change your URL’s.  So if you want to use this code:

1. Visit https://developer.apps.yahoo.com/

2. Create a Anonymous Project.

3. Get your Key

4. Insert your key in the first line of code below.

5. Place this on the OnChange Event for the Zip Code Field for either the contact or account entity.

 

Enjoy!

 

Ben

var sUrl = http://local.yahooapis.com/MapsService/V1/geocode?appid=INSERT YOUR APP ID HERE!!!;
sUrl += "&zip=" + crmForm.all.address1_postalcode.DataValue;
var oXmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
oXmlHTTP.Open("GET", sUrl, false);
oXmlHTTP.Send();
var oXmlDoc = oXmlHTTP.responseXML;
crmForm.all.address1_line1.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/Address").text
crmForm.all.address1_city.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/City").text
crmForm.all.address1_stateorprovince.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/State").text
crmForm.all.address1_country.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/Country").text

Tags: , , ,

Demo Tools

Powered by BlogEngine.NET 1.6.1.0
Original Design by Laptop Geek, Adapted by onesoft