Sunday, August 30, 2009

Generic method for Ajax calls using javascript to retrieve data in MS CRM 4.0

//limitation – This is the generic method supported only for 1:N or N:1 relationship



// This is a generic method for Ajax calls using javascript to retrieve data
//****************************************************************************
SoapRequestDynamic = function(entityName, searchColumn, searchValue, returnColumn)
{
var returnPacket = "";
for (var i = 0; i < returnColumn.length; i++)
{
returnPacket += " <q1:Attribute>" + returnColumn[i] + "</q1:Attribute>"
}

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\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>" + entityName + "</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
returnPacket +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>" + searchColumn + "</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + searchValue + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";

return xml;
}
// End of SoapRequestDynamic Function

// Start of HttpPostRequest
HttpPostRequest = function(xml)
{
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);

//alert(xmlHttpRequest.responseXML.xml);
var resultXml = xmlHttpRequest.responseXML.selectSingleNode("//BusinessEntity");

return resultXml;
}
// End of HttpPostRequest

var sObjectID = crmForm.ObjectId;

//Actual Call for Relationships
/// finding out opportunity relationships
//var buXml = HttpPostRequest(SoapRequestDynamic(entityName, searchColumn, searchValue, returnColumn));

var buXml = HttpPostRequest(SoapRequestDynamic("customeropportunityrole", "opportunityid", sObjectID, ["customeropportunityroleid"]));


if(buXml != null )
{
var buNodes = buXml.selectNodes("//BusinessEntity/q1:customeropportunityroleid");
if(buNodes.length > 0)
{
// DEFINE YOUR LOGIC HERE
}
}



// for your referance
//http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/3ec3cfcd-62ea-49b5-9239-14aee58bd7b9

No comments: