Help me in using fetchxml in javascript, retrieve CRM 2011 attribute values

Jun 14, 2011 at 6:38 PM

I have two custom entities named Student and Teacher. In Teacher entity, teacher's informations like address,

mobile no are there and in Student entity, student's information is there. In student form, there is a lookup field

mapped with teacher, and there is a teachers mobile no field in student form. When a teacher is selected in the lookup,

I want that teacher's mobile no to be populated in that mobile no. field of student form.

I used fetchXML in JavaScript in the onchange property of the teacher lookup, but it is not working.

No response is coming back. My code is somewhat like:

function fetch()
var lookupItem = new Array;
lookupItem = Xrm.Page.getAttribute("custom_teacher").getValue();
var val = lookupItem[0].name;
// Prepare variables 
var fetchMapping = "logical";
var entityName = "custom_student";//student entity
var FirstColumn = "custom_name"; //student field
var SecondColumn = "custom_class";//student field
var ThirdColumn = "custom_roll";//student field

var ForthColumn = "custom_mobno";//teacher field
var FifthColumn = "custom_teachername";//teacher field

var linkEntity = "custom_teacher";//teacher entity

var linkEntityFrom = "custom_mobno";//teacher field
var linkEntityTo ="custom_teachermob";//student field

var filterType = "and"; //filter type
var conditionAttribute = "custom_mobno";//teacher field

var operator = "eq"; //operator
var value = val; //value for filtering 

var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//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;"+  



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";
//var msg = results.length;
    for (i=0;i < results.length;i++)
     var attribute1 = results[i].selectSingleNode('./custom_name').nodeTypedValue;
     var attribute2 = results[i].selectSingleNode('./custom_class').nodeTypedValue;
     var attribute3 = results[i].selectSingleNode('./custom_roll').nodeTypedValue;
     var linkedattribute1 = results[i].selectSingleNode('./custom_teacher.custom_mobno').nodeTypedValue;
    // var linkedattribute2 = results[i].selectSingleNode('./custom_producttype.custom_teachername').nodeTypedValue;
     msg += attribute1 + "\t" + attribute2 + "\t" + attribute3 + "\t" + linkedattribute1 + "\t" + "\r";
    alert(msg); //alert the fetch result