Wednesday, December 23, 2009

MS CRM 4.0 - Call RetrieveMultiple webservice in JavaScript helper function

I've developed a function to call the CRM RetrieveMultiple webservice in JavaScript without having to write the XML yourselve: "CallRetrieveMultipleWebService".

The function generates the XML for you. You just have to pass the entity to query, an array of columns to get, the filter operator and the array of conditions.

Here's the function with an example how to call the webservice to find contacts with a first- and lastname as conditions.




function CallRetrieveMultipleWebService(queryEntityName, queryColumnSetAttributes, queryFilterOperator, conditionArray) {
if (queryEntityName != null && queryColumnSetAttributes != null) {
var columnSetAttributesString = "";
for (var i = 0; i < queryColumnSetAttributes.length; i++) {
columnSetAttributesString += "" + queryColumnSetAttributes[i] + "";
}
var conditionsString = "";
if (conditionArray != null) {
conditionsString = "";
for (var i = 0; i < conditionArray.length; i++) {
conditionsString += "";
conditionsString += "" + conditionArray[i][0] + "";
conditionsString += "" + conditionArray[i][1] + "";
conditionsString += "";
var valuesArray = conditionArray[i][2];
for (var j = 0; j < valuesArray.length; j++) {
conditionsString += "" + valuesArray[j] + "";
}
conditionsString += "
";
conditionsString += "
";
}
conditionsString += "
";
}

var xml = "" +
"" +
"" +
GenerateAuthenticationHeader() +
" " +
" " +
" " +
" " + queryEntityName + "" +
" " +
" " +
columnSetAttributesString +
"
" +
"
" +
"" +
"" + queryFilterOperator + "" +
conditionsString +
"
" +
"
" +
"
" +
"
" +
"
" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
// return results
else {
return resultXml;
}
}
}

//function to find a contact, using the CallRetrieveMultipleWebService function:
function FindContact(firstnameToSearch, lastnameToSearch)
{
//define the entity name to query
var queryEntityName = "contact";
//set up the array of columns to get
var queryColumnSetAttributes = new Array();
queryColumnSetAttributes[0] = "contactid";
queryColumnSetAttributes[1] = "telephone1";
//define the filter operator
var queryFilterOperator = "And";
//set up the array of confitions in the query
var queryConditions = new Array();
//define 2 conditions: add 2 items to the array:
queryConditions[0] = new Array("firstname", "Equal", new Array(firstnameToSearch));
queryConditions[1] = new Array("lastname", "Equal", new Array(lastnameToSearch));
//Call function CallRetrieveMultipleWebService
var resultXml = CallRetrieveMultipleWebService(queryEntityName, queryColumnSetAttributes, queryFilterOperator, queryConditions);
var teleponeValueToSet = null;
var results = resultXml.getElementsByTagName('BusinessEntity');
if (results.length > 0) {
//do something with the found contacts
//example: show an alert with the telephone number of the first found contact
var telephone = results[0].selectSingleNode('./q1:telephone1') == null ? null : results[0].selectSingleNode('./q1:telephone1').nodeTypedValue;
if (telephone != null) {
alert(telephone);
}
}
else {
alert('No contact ' + firstnameToSearch + ' ' + lastnameToSearch + ' found.');
}
}

2 comments:

Anonymous said...

This is great, however I am being prompted for a username and password when the code gets to xmlHttpRequest.

Any ideas?

digital signature said...

Hi there, I am new to Java and I really appreciate all this great information. Thanks for it.