Monday, December 15, 2008

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

function DeleteEntityRecord(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 +="   #Delete xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
      xmlString +="     #entity xsi:type='" + entityName + "'>";
      xmlString +=  attributes;
      xmlString +="     #/entity>";
      xmlString +="   #/Delete>";
      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/Delete");
      oXmlHttp.send(sXml);
      
  var results = xmlHttpRequest.responseXML;
}

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

1 comment:

Anonymous said...

This type of delete method doesn't work as of 7/2011. The webservice only allows for deleting records one record at at time.

See http://msdn.microsoft.com/en-us/library/cc677071.aspx