Wednesday, May 18, 2011

CRM FetchXml with Javascript

FetchXML method using javascript in CRM, allows me to use linked entities, so I can query attributes from different tables and linke them together with some primary attribute. 

Here's example:

// Prepare variables
var fetchMapping = "logical";
var entityName = "entitiyname";
//first attribute to fetch from this entity [CHANGE THIS]
var FirstColumn = "attributename1"; //first attribute to fetch from this entity [CHANGE THIS]
var SecondColumn = "attributename2"; //second attribute to fetch from this entity [CHANGE THIS]
var ThirdColumn = "attributename3"; //third attribute to fetch from this entity [CHANGE THIS]

var ForthColumn = "linkedattributename1"; //first attribute to fetch from linked entity [CHANGE THIS]
var FifthColumn = "linkedattributename2"; //second attribute to fetch from linked entity [CHANGE THIS]

var linkEntity = "linkentityname"; //linked entity name [CHANGE THIS]
// These both attributenames should have same value to link entities [CHANGE THIS]
var linkEntityFrom = "linkentity_attributename";
var linkEntityTo ="currententity_attributename";

var filterType = "and"; //filter type
var conditionAttribute = "attributename"; //condition attribute from linked entity [CHANGE THIS]
var operator = "eq"; //operator
var value = "value"; //value for filtering [CHANGE THIS]

var authenticationHeader = GenerateAuthenticationHeader();
//SOAP message
var xml = "<?xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap=''"+
" xmlns:xsi=''"+
" xmlns:xsd=''>"+
"<Fetch xmlns=''>"+
"<fetchXml>&lt;fetch mapping='"+fetchMapping+"'&gt;"+
"&lt;entity name='"+entityName+"'&gt;"+
"&lt;attribute name='"+FirstColumn+"'/&gt;"+
"&lt;attribute name='"+SecondColumn+"'/&gt;"+
"&lt;attribute name='"+ThirdColumn+"'/&gt;"+

"&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
"&lt;attribute name='"+ForthColumn+"'/&gt;"+
"&lt;attribute name='"+FifthColumn+"'/&gt;"+

"&lt;filter type='"+filterType+"'&gt;"+
"&lt;condition attribute='"+conditionAttribute+"'"+
" operator='"+operator+"' value='"+value+"'/&gt;"+ 

// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);

var resultXml = xHReq.responseXML;

var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
var msg =   resultXml.selectSingleNode('//description').nodeTypedValue;
var resultSet = new String();
resultSet = resultXml.text;

var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
var results = oXmlDoc.getElementsByTagName('result');

var msg = "Result\r";

    for (i=0;i < results.length;i++)
     var attribute1 = results[i].selectSingleNode('./attributename1').nodeTypedValue; //[CHANGE THIS]
     var attribute2 = results[i].selectSingleNode('./attributename2').nodeTypedValue; //[CHANGE THIS]
     var attribute3 = results[i].selectSingleNode('./attributename3').nodeTypedValue; //[CHANGE THIS]
     var linkedattribute1 = results[i].selectSingleNode('./currententity_attributename.
linkedattributename1').nodeTypedValue; //[CHANGE THIS]
     var linkedattribute2 = results[i].selectSingleNode('./currententity_attributename.
linkedattributename2').nodeTypedValue; //[CHANGE THIS]
     msg += attribute1 + "\t" + attribute2 + "\t" + attribute3 + "\t" + linkedattribute1 + "\t" + linkedattribute2 + "\r";
    alert(msg); //alert the fetch result

No comments:

Post a Comment