My blog has moved!

Please visit
http://crmdude.wordpress.com/
and update your bookmarks.

Tuesday, 13 January 2009

Performing a JavaScript web service call

The following example shows to perform a web service call to work out whether the Potential Customer of an Opportunity is on Credit Stop or not. It should be placed in the OnChange event of the Customer field on the Opportunity record.



//Prepare variables for a account to retrieve.
var accountid = crmForm.all.customerid.DataValue[0].id
var authenticationHeader = GenerateAuthenticationHeader();

//Prepare the SOAP message.
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'>"+
authenticationHeader+
"<soap:Body>"+
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entityName>account</entityName>"+
"<id>"+accountid+"</id>"+
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>creditonhold</q1:Attribute>"+
"<q1:Attribute>name</q1:Attribute>"+
"</q1:Attributes>"+
"</columnSet>"+
"</Retrieve>"+
"</soap:Body>"+
"</soap:Envelope>";
//Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
//Capture the result.
var resultXml = xHReq.responseXML;


No comments:

Post a Comment