Accessing Web Services

banner art

The most efficient way to access Microsoft CRM Web services is to create a Web service on the computer running Microsoft CRM and connect to it from your client side JScript code. This is preferred because you can write the majority of your code within the Web Service in Visual C# or Visual Basic .NET using IntelliSense. However, it is possible for you to access the Microsoft CRM Web services from your JScript code. For more information about Microsoft CRM Web services, see CrmService Web Service and MetadataService Web Service.

The following example accesses a public Web service, called webservicex.net, to get the exchange rate between U.S. dollars and a chosen currency. It references the following three fields that must be added to the Invoice entity using the Customization tools in Microsoft CRM 3.0:

  • new_currency, which allows selection of the currency to convert
    • Type = picklist
    • Value and Label:
      • 1 = "JPY"
      • 2 = "GBP"
      • 3 = "EUR"
  • new_exchangerate, which shows the exchange rate
    • Type = float
    • Precision = 2
    • Minimum Value = 0.0
    • Maximum Value = 1,000,000,000.00
  • new_totalcurrency, which shows the total amount in the selected currency
    • Type = float
    • Precision = 2
    • Minimum Value = 0.0
    • Maximum Value = 1,000,000,000.00

Example

Paste the following code example into the OnChange event for the new_currency picklist field. The code executes when the currency is changed.

// Declare the variables.
var sCurrency=""; 
var mTotalAmount = crmForm.all.totalamount.DataValue;
var mTotalCurrency = 0; 
var serverUrl = "https://www.webservicex.net";
var serviceLocation = "/CurrencyConvertor.asmx/ConversionRate?";
var serviceParameters = "FromCurrency=USD&ToCurrency=";
var startTag = "<double xmlns=\"https://www.webserviceX.NET/\">";
var endTag = "</double>";
var exch = "";
var valueStart = "";
var valueEnd = "";

// This is the picklist.
switch (parseInt(event.srcElement.DataValue, 10))
{ 
   case 1: 
      sCurrency = "JPY"; 
      break; 
   case 2: 
      sCurrency = "GBP"; 
      break; 
   case 3: 
      sCurrency = "EUR"; 
      break; 
} 

// Instantiate at connection to the Web service and call the get method.
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("get", serverUrl + serviceLocation + serviceParameters + escape(sCurrency), false);
xmlhttp.send();

// Parse the returned XML string.
valueStart = xmlhttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;
valueEnd = xmlhttp.responseXml.xml.indexOf(endTag, valueEnd+1);
exch = xmlhttp.responseXML.xml.substring(valueStart, valueEnd);

// Set the exchange rate on the custom attribute.
crmForm.all.new_exchangerate.DataValue = parseFloat (exch);

// Calculate and set the total sum in the selected currency.
mTotalCurrency = mTotalAmount*(parseFloat(exch));
crmForm.all.new_totalcurrency.DataValue = mTotalCurrency;


© 2007 Microsoft Corporation. All rights reserved.