Accessing Web Services From CRM Forms

I have been very busy planning for our next release and I have to say, I am very excited about what we are doing next! More on that later........... I am still getting mail from folks asking how to access a web service from CRM forms. I thought I share a little JScript code that I used for some of my demos. This code accesses a public web service to get the exchange rate between US$ and a given currency. I did a MSN search and found a public web service; webservicex.net; that provides a public currency converter web service. Then I added one picklist and two fields to the Invoice Business Entity using the Customization tools in CRM V3.0: fabrikam_exchangerate (shows the exchange rate) and fabrikam_totalcurrency (shows the total amount in the selected currency).

Here is the code followed by a snapshot of the Customized CRM Invoice form. You can easily copy and paste this code into your OnChange, OnSave or OnLoad form event using the CRM V3.0 form editor. I added the code as an OnChange event to a new picklist (target currency) that I added earlier to the form.

// Declaring Some variable here including the web service to connect to

var

i=0; var j=0; var k=0; var r=0;

var

serverUrl = "https://www.webservicex.net";

//This is my picklist. I have already added three currency that i want to support the exchange to

switch

(parseInt(event.srcElement.DataValue, 10))

{

case 1:

i = "JPY";

break;

case 2:

i = "GBP";

break;

case 3:

i = "EUR";

break;

}

//Instantiating connection to the web service and calling the get method

var

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.open("get", serverUrl + "/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency="+escape(i), false);

xmlhttp.send();

var

startTag = "<double xmlns=\"https://www.webserviceX.NET/\">";

var

endTag = "</double>";

var

exch;

var

valueStart = 0;

var

valueEnd = 0;

//Parsing the returned XML

valueStart = xmlhttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;

valueEnd = xmlhttp.responseXml.xml.indexOf(endTag, valueEnd+1);

exch = xmlhttp.responseXML.xml.substring(valueStart, valueEnd);

//Setting the Exchange rate on the custom attribute that i have added for this purpose earlier

crmForm.all.fabrikam_exchangerate.DataValue = parseFloat (exch);

j = crmForm.all.totalamount.DataValue;

var

kk = j*(parseFloat(exch));

//Calculating and setting the total sum in the selected currency

crmForm.all.fabrikam_totalcurrency.DataValue = kk;

CustomizedInvoiceForm.JPG