Tuesday, November 13, 2007

Getting Multiple attibutes from a related entity

The following function can be used to retrieve multiple attributes from a related entity. It returns an xml document which can then be parsed to place into the currently open document.

I have included an eample of how this can be used here.

You will need to replace the [ ] chars with < > (done to get around some blogger limitations


//----------------------------------------------------------
function getattribsByLookup(entityForList, attribForFilter, idToFetch, attribsToReturn ) {
// attribs to return is an array
//----------------------------------------------------------
var f = crmForm.all;
var attribsList = '';
for( i = 0; i [ attribsToReturn.length; i++ ) {
attribsList += " [q1:Attribute]" + attribsToReturn[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\"]" +
" [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]" + entityForList + "[/q1:EntityName]" +
" [q1:ColumnSet xsi:type=\"q1:ColumnSet\"]" +
" [q1:Attributes]" +
attribsList +
" [/q1:Attributes]" +
" [/q1:ColumnSet]" +
" [q1:Distinct]true[/q1:Distinct]" +
" [q1:Criteria]" +
" [q1:FilterOperator]And[/q1:FilterOperator]" +
" [q1:Conditions]" +
" [q1:Condition]" +
" [q1:AttributeName]" + attribForFilter + "[/q1:AttributeName]" +
" [q1:Operator]Equal[/q1:Operator]" +
" [q1:Values]" +
" [q1:Value xsi:type=\"xsd:string\"]" + idToFetch + "[/q1:Value]" +
" [/q1:Values]" +
" [/q1:Condition]" +
" [/q1:Conditions]" +
" [/q1:Criteria]" +
" [/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);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
};

No comments: