Monday, December 15, 2008

How to update records from CRM web forms using javascript in MS CRM 4.0

function UpdateEntityRecord(entityName, attributes, orgName)
{
      //attributes = ?accountid> 92384923 ?/accountid>?firstname>harry?/firstname> ...
  var xmlString = "??sXml version='1.0' encoding='utf-8'?>";
      xmlString +="?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'>";
      xmlString +="?soap:Header>?CrmAuthenticationToken xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
      xmlString +="?AuthenticationType xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>0?/AuthenticationType>";
      xmlString +="?OrganizationName xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>" + orgName + "?/OrganizationName>";
      xmlString +="?CallerId xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>00000000-0000-0000-0000-000000000000?/CallerId>";
      xmlString +="?/CrmAuthenticationToken>";
      xmlString +="?/soap:Header>";
      xmlString += " ?soap:Body>" +
      xmlString +="   ?Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
      xmlString +="     ?entity xsi:type='" + entityName + "'>";
      xmlString +=  attributes;
      xmlString +="     ?/entity>";
      xmlString +="   ?/Update>";
      xmlString +=" ?/soap:Body>";
      xmlString +="?/soap:Envelope>";


      oXmlHttp.open("POST", "http://localhost/mscrmservices/2007/crmservice.asmx",false);
      oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      oXmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Update");
      oXmlHttp.send(sXml);
      
  var results = xmlHttpRequest.responseXML;
}

Please replace '?' with '<' after copying and before pasting..:)

No comments: