Monday, June 28, 2010

CRM update web service call in Java Script in MS CRM 4.0

// Record Owner
var Owner = new Array;
Owner = crmForm.all.ownerid.DataValue;
var oppOwner = Owner[0].name;

// updating Opportunity
/* setting opportunuty owner name to a text field in the same record using crm update web service call in JScript */
try
{
var xml1 = "<?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>"+
"<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entity xsi:type='opportunity'>" +
"<new_text>"+ xmlencode(oppOwner) +"</new_text>" +
"<opportunityid>" + crmForm.ObjectId + "</opportunityid>" +
"</entity>"+
"</Update>"+
"</soap:Body>"+
"</soap:Envelope>";

var xHReq1 = new ActiveXObject("Msxml2.XMLHTTP");
xHReq1.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq1.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update");
xHReq1.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq1.setRequestHeader("Content-Length", xml1.length);
xHReq1.send(xml1);
// Capture the result
var resultXml1 = xHReq1.responseXML;

var errorCount1 = resultXml1.selectNodes('//error').length;
}
catch (e)
{ }

/* xml encoding to convert "&" and single quote (') symbol*/
function xmlencode(string) {
return string.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;').replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
}

No comments: