Joseph Michael Pesch
VP Programming

MS CRM GetBusinessEntity JavaScript SOAP Call

by 3. November 2010 17:33

window.IsNull = IsNull;

function IsNull(value) {

  return (typeof value == 'undefined' || value === null);

}

 

window.GetBusinessEntity = GetBusinessEntity;

function GetBusinessEntity(sEntityname, sAttribute, sLookupValue) {

  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\">" +

  " <soap:Body>" +

  " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +

  " <q1:EntityName>" + sEntityname + "</q1:EntityName>" +

  " <q1:ColumnSet xsi:type=\"q1:AllColumns\" />" +

  " <q1:Distinct>false</q1:Distinct>";

  if (!IsNull(sAttribute)) {

    xml +=

    " <q1:Criteria>" +

    " <q1:FilterOperator>And</q1:FilterOperator>" +

    " <q1:Conditions>" +

    " <q1:Condition>" +

    " <q1:AttributeName>" + sAttribute + "</q1:AttributeName>" +

    " <q1:Operator>Equal</q1:Operator>" +

    " <q1:Values>" +

    " <q1:Value xsi:type=\"xsd:string\">" + sLookupValue + "</q1:Value>" +

    " </q1:Values>" +

    " </q1:Condition>" +

    " </q1:Conditions>" +

    " </q1:Criteria>";

  }

  xml +=

  " </query>" +

  " </soap:Body>" +

  "</soap:Envelope>" + "";

 

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

  xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);

  xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple");

  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

  xmlHttpRequest.send(xml);

  return xmlHttpRequest.responseXml;

 

}

 

window.GetBusinessEntitySample = GetBusinessEntitySample;

function GetBusinessEntitySample() {

 

  var xml;

 

  // Get a specific Business Entity by Id

  xml = GetBusinessEntity('new_lkploantransactiondispotype', 'new_lkploantransactiondispotypeid', 'A73110D8-A4E6-DF11-8A19-0050568619C7');

  alert('The name of selected id search is: ' + xml.selectNodes("//BusinessEntity")[0].selectSingleNode("./q1:new_name").text);

 

  // Get a specific Business Entity by Name

  xml = GetBusinessEntity('new_lkploantransactiondispotype', 'new_name', 'Lead');

  alert('There id of the selected name search is: ' + xml.selectNodes("//BusinessEntity")[0].selectSingleNode("./q1:new_lkploantransactiondispotypeid").text);

 

  // Get all active Business Entities

  xml = GetBusinessEntity('new_lkploantransactiondispotype', 'statuscode', '1');

  var nodeList = xml.selectNodes("//BusinessEntity");

  var msg = 'There are : ' + nodeList.length + ' active entities';

  for (i = 0; i < nodeList.length; i++) {

    rsName = nodeList[i].selectSingleNode("./q1:new_name").text;

    rsGuid = nodeList[i].selectSingleNode("./q1:new_lkploantransactiondispotypeid").text;

    msg += '\r\n' + rsGuid + ": " + rsName

  }

  alert(msg);

 

  // Get all Business Entities

  xml = GetBusinessEntity('new_lkploantransactiondispotype');

  var nodeList = xml.selectNodes("//BusinessEntity");

  var msg = 'There are : ' + nodeList.length + ' total entities';

  for (i = 0; i < nodeList.length; i++) {

    rsName = nodeList[i].selectSingleNode("./q1:new_name").text;

    rsGuid = nodeList[i].selectSingleNode("./q1:new_lkploantransactiondispotypeid").text;

    msg += '\r\n' + rsGuid + ": " + rsName

  }

  alert(msg);

 

}

 

 

 

Tags:

[None]

Comments are closed