Welcome to MSDN Blogs Sign in | Join | Help

Arash Ghanaie-Sichanie Blog

Microsoft, Program Management, Live Services, Software + Service and more....
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 = "http://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=\"http://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;

Posted: Wednesday, February 01, 2006 4:50 AM by ArashSichanie

Attachment(s): CustomizedInvoiceForm.JPG

Comments

CRM Developer said:

Arash Sichanie has an excellent post on using Javascript to access a web service from a CRM form. Great stuff. His example shows accessing a web service to get the exchange rate between USD and the selected currency type.
# February 7, 2006 5:25 PM

Ryan said:

might this be used to access the crmservice from client-side code as well?
# February 27, 2006 3:10 PM

Rashmi said:

Its a really great article...
But i seem to have trouble implementing it as i am new to CRM
# March 8, 2006 4:59 AM

ArashSichanie said:

Ryan
You can do that but Jscript is not the best way to access rich web services with complext types.  An alternative approach would be to build a web service on your CRM server to do the leg work and connect to it via the above code.  That way you can write your CRM web service access in C# using rich IntelliSense.  If you really want to call CRM web Services using Jscript you still can do that but you need to write more code to generate SOAP calls.
# March 14, 2006 5:58 PM

Microsoft Dynamics CRM @ Joris Kalz's WebLog said:


Hello JScript friends,
I am back from vacation and back again to enjoy you with some interesting...
# July 16, 2006 11:23 AM

dejavu said:

I have a confusion...this code is SENDING or RETRIEVING DATA from the web service? i Can see that is sending some value and returning some other value, depending on what value we are sendig right?.

Well, i have a script that is not working. It sends from crm to a custom web service a value, and the webservice process it and return to the same form in crm (in other txtbox) a new diferent value. Ex: if i send 5, the web service return "five".

This example, can it be done or it's me with a illusion about the cpabilities of crm? i haven't do it work.

Regards.
# August 24, 2006 3:29 PM

Arash Ghanaie-Sichanie Blog said:

Since&#160;I wrote the post about calling &quot;any&quot; web service from CRM forms (BTW, that is my most viewed post...
# August 28, 2006 10:27 PM

Vinod Madane said:

This is very Helpfull
# September 12, 2006 7:12 AM

Alonso Brenes said:

Is there any special configuration or requeriment to run this code?

I cant make it work...
# September 28, 2006 12:06 PM

Microsoft Dynamics CRM @ Joris Kalz's WebLog said:

Hello JScript friends, I'm back from vacation and back again to enjoy some interesting jscript stuff

# October 30, 2006 4:17 PM

Rob Schripsema said:

OK, this is great, and it appears to be the sample code that is found in the Microsoft CRM SDK documentation.

I've written a custom web service in VS 2005 that I wish to call in the same manner from a CRM form.  The form captures an integer field and needs to pass it to the web service, which validates it against another table, and return a true/false.

I don't believe one can pass the function name and the parameters to a SOAP web service using the URL syntax that you include in the sample code, above. At least not in the default manner that VS 2005 creates web services.

What needs to be done in order to get the full XML SOAP request structure into the call to the web service? What am I missing here?

# December 11, 2006 11:43 AM

East Region Microsoft CRM said:

My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from

# March 16, 2007 1:22 PM

Faisal Fiaz said:

This code solved alot of our problems. Thank you very much

# April 13, 2007 5:22 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker