There are many samples on blogs or in newsgroups accessing the CRM web services from JavaScript that are based on the output of either the Stunnware Tools or the .NET to JavaScript conversion tool and I think it's time to start a new series collecting these samples.
The first is based on a newsgroup post and shows how to retrieve the exchange rate of a foreign currency. To make it easier to reuse, I put the main code into a function:
/// <summary>Returns the exchange rate of a foreign currency as stored in the CRM currency table.</summary>
/// <param name="isoCurrencyCode" type="String">The currency code, e.g. USD or EUR.</param>
/// <returns type="Number">The exchange rate.</returns>
function GetExchangeRate(isoCurrencyCode) {
/* Setup the SOAP message. You can easily build this request with the Stunnware Tools
http://www.stunnware.com/?area=products&group=swtools4
or use the .NET to JavaScript conversion tool
http://www.stunnware.com/crm2/topic.aspx?id=JSWebService2
*/
var xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>transactioncurrency</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>exchangerate</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>isocurrencycode</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + isoCurrencyCode + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>";
/* Setup and execute the web request. */
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
/* Parse and return the result. Again look at the output of the tools mentioned above to build the correct query. */
var resultXml = xmlHttpRequest.responseXML;
var exchangeRateText = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity/q1:exchangerate").text;
return parseFloat(exchangeRateText);
}
Calling the function is quite simple. Just pass the currency code as a parameter and the exchange rate is returned:
alert(GetExchangeRate("USD"));
alert(GetExchangeRate("AUD"));