Share via


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

Comments

  • Anonymous
    February 07, 2006
    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.
  • Anonymous
    February 27, 2006
    might this be used to access the crmservice from client-side code as well?
  • Anonymous
    March 07, 2006
    Its a really great article...
    But i seem to have trouble implementing it as i am new to CRM
  • Anonymous
    March 14, 2006
    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.
  • Anonymous
    July 16, 2006

    Hello JScript friends,
    I am back from vacation and back again to enjoy you with some interesting...
  • Anonymous
    August 24, 2006
    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.
  • Anonymous
    August 28, 2006
    Since&#160;I wrote the post about calling &quot;any&quot; web service from CRM forms (BTW, that is my most viewed post...
  • Anonymous
    September 11, 2006
    This is very Helpfull
  • Anonymous
    September 28, 2006
    The comment has been removed
  • Anonymous
    October 30, 2006
    Hello JScript friends, I'm back from vacation and back again to enjoy some interesting jscript stuff
  • Anonymous
    December 11, 2006
    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?
  • Anonymous
    March 16, 2007
    My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from
  • Anonymous
    April 12, 2007
    This code solved alot of our problems. Thank you very much
  • Anonymous
    May 28, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=arash-ghanaie-sichanie-blog-accessing-web-services-from-crm-forms