Retrieve multiple records from a link entity using JavaScript in MS CRM 4.0
Here is a JavaScript method which can be used to retrieve multiple records from a link entity using CRM Web Service. This method can be used to retrieve data for any CRM entity. It requires following parameters to be passed:
1. entityName: Then schema name of an entity to be retrieved.
2. arrColumnName: Array of attributes to be retrieved.
3. enableDistinct: Boolean value to get distinct records form an entity.
4. linkFromEntityName: Name of linked entity
5. linkfromAttributeName: Attribute name from linked entity that is referenced in relationship table.
6. linkToEntityName: Name of relationship table which is created automatically by CRM
7. linkToAttributeName: Attribute name from relationship table that is reference attribute for linked entity.
8. linkEntityFilterAttributeName: Attribute name from relationship table on which records are filtered.
9. linkEntityFilterAttributeValue: Value for the linkEntityFilterAttributeName attribute that is used to filter records.
function RetrieveMultipleFromLinkEntity(entityName , arrColumnName, enableDistinct, linkFromEntityName,linkFromAttributeName, linkToEntityName,linkToAttributeName, linkEntityFilterAttributeName,linkEntityFilterAttributeValue)
{
if((entityName != null) && (entityName.length > 0) && (arrColumnName != null) && (arrColumnName.length > 0)
(linkFromEntityName != null) && (linkFromEntityName.length > 0) && (linkFromAttributeName != null) && (linkFromAttributeName.length > 0)
(linkToEntityName != null) && (linkToEntityName.length > 0) && (linkToAttributeName != null) && (linkToAttributeName.length > 0)
(linkEntityFilterAttributeName != null) && (linkEntityFilterAttributeName.length > 0) && (linkEntityFilterAttributeValue != null) && (linkEntityFilterAttributeValue.length > 0))
{
var ColumnsSetAttributes = '';
var IsDisinctValue = false;
// Build columns to be retrieved
for(var i = 0; i < arrColumnName.length; i++)
{
ColumnsSetAttributes += " <q1:Attribute>" + arrColumnName[i] + "</q1:Attribute>"
}
//Set Distinct value
if(enableDistinct != null && typeof(enableDistinct) != "undefined" && (enableDistinct.toLowerCase() == "true" || enableDistinct == true))
{
IsDistinctValue = true;
}
// Build link entity filter
var LinkFilterCondition = " <q1:Condition>"+
" <q1:AttributeName>" + linkEntityFilterAttributeName + "</q1:AttributeName>"+
" <q1:Operator>Equal</q1:Operator>"+
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">"+ linkEntityFilterAttributeValue +"</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>";
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>" +
ColumnsSetAttributes +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>" + IsDistinctValue + "</q1:Distinct>" +
" <q1:LinkEntities>" +
" <q1:LinkEntity>" +
" <q1:LinkFromAttributeName>" + linkFromAttributeName + "</q1:LinkFromAttributeName>" +
" <q1:LinkFromEntityName>" + linkFromEntityName + "</q1:LinkFromEntityName>" +
" <q1:LinkToEntityName>" + linkToEntityName + "</q1:LinkToEntityName>" +
" <q1:LinkToAttributeName>" + linkToAttributeName + "</q1:LinkToAttributeName>" +
" <q1:JoinOperator>Inner</q1:JoinOperator>" +
" <q1:LinkCriteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
LinkFilterCondition +
" </q1:Conditions>" +
" </q1:LinkCriteria>" +
" </q1:LinkEntity>" +
" </q1:LinkEntities>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>";
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);
var oXml;
try
{
xmlHttpRequest.send(xml);
oXml = xmlHttpRequest.responseXML;
// Check for errors.
if(oXml != null)
{
var errorCount = oXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = oXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
else
{
//return the Business Entity node
return oXml.selectNodes("//BusinessEntity");
}
}
else
{
alert("Error: No response received.");
}
}
catch(e)
{
if(oXml != null)
{
var err = oXml.getElementsByTagName("description")[0].childNodes[0].nodeValue;
alert("Error: " + e.description + ": " + err);
}
else
{
alert("Error: Unexpected error occurred.");
}
}
}
return null;
}
Example:
To retrieve lead’s full name associated with contact entity.
//Example: To retrieve leads associated with contact
var EntityName = "lead";
var AttributeList = new Array();
AttributeList[0] = "leadid";
AttributeList[1] = "fullname";
var RetrieveDistinct = false;
var LinkFromEntityName = "lead";
var LinkFromAttributeName = "leadid";
var LinkToEntityName = "contactleads";
var LinkToAttributeName = "leadid"
var LinkEntityFilterAttributeName = "contactid";
var LinkEntityFilterAttributeValue = ""; //set the contact GUI
var responseXml = RetrieveMultipleFromLinkEntity(EntityName,AttributeList, false,LinkromEntityName
,LinkFromAttributeName, LinkToEntityName,LinkToAttributeName
,LinkEntityFilterAttributeName,LinkEntityFilterAttributeValue);
if(responseXml != null)
{
for(i = 0; i < responseXml.length; i++)
{
var LeadId = responseXml[i].childNodes[0].text;
var LeadName = responseXml[i].childNodes[1].text;
}
}
No comments:
Post a Comment