Update parent customerid in Contact using Soap Request

If you are looking to update parent customer id in contact record using soap request you can use below code. I have used Soaplogger application that comes with Microsoft CRM 2011 SDK to generate this soap request, you can simply write your server side code in soaploagger and run this application, it will generate client slide soap request for you.

function UpdateParentCustomer(ParentCustomerID,ContactID)
{
var _ServerURL = Xrm.Page.context.getServerUrl() + “/XRMServices/2011/Organization.svc/web”;
var SoapRequest=”<s:Envelope xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’>”+
“<s:Body><Update xmlns=’http://schemas.microsoft.com/xrm/2011/Contracts/Services’ xmlns:i=’http://www.w3.org/2001/XMLSchema-instance’>”+
“<entity xmlns:a=’http://schemas.microsoft.com/xrm/2011/Contracts’>”+
“<a:Attributes xmlns:b=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’>”+
“<a:KeyValuePairOfstringanyType>”+
“<b:key>parentcustomerid</b:key>”+
“<b:value i:type=’a:EntityReference’>”+
“<a:Id>”+ParentCustomerID+”</a:Id>”+
“<a:LogicalName>account</a:LogicalName>”+
“<a:Name i:nil=’true’ />”+
“</b:value>”+
“</a:KeyValuePairOfstringanyType>”+
“<a:KeyValuePairOfstringanyType>”+
“<b:key>contactid</b:key>”+
“<b:value i:type=’c:guid’ xmlns:c=’http://schemas.microsoft.com/2003/10/Serialization/’>”+ContactID+”</b:value>”+
“</a:KeyValuePairOfstringanyType>”+
“</a:Attributes>”+
“<a:EntityState i:nil=’true’ />”+
“<a:FormattedValues xmlns:b=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’ />”+
“<a:Id>00000000-0000-0000-0000-000000000000</a:Id>”+
“<a:LogicalName>contact</a:LogicalName>”+
“<a:RelatedEntities xmlns:b=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’ />”+
“</entity></Update></s:Body></s:Envelope>”;
var req = new XMLHttpRequest();
req.open(“POST”, _ServerURL, true)
req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Update”);
req.onreadystatechange = function () { Soap_Callback(req); };
req.send(SoapRequest);
}
function Soap_Callback(req)
{
if (req.readyState == 4) {
if (req.status == 200) {
alert(“Updated record successfully”);  }
else {var resultXml = req.responseXML;
var bodyNode = resultXml.firstChild.firstChild;
//Retrieve error message
for (var i = 0; i < bodyNode.childNodes.length; i++) {
var node = bodyNode.childNodes[i];
if (“s:Fault” == node.nodeName) {
for (var j = 0; j < node.childNodes.length; j++) {
var faultStringNode = node.childNodes[j];
if (“faultcode” == faultStringNode.nodeName) {
alert(faultStringNode.textContent);
break;
}
}
break;
}
}
}
}
}
Hope it will help someone !!!

 

Leave a Reply

Your email address will not be published. Required fields are marked *