Pulling Data from a Related Entity in CRM 4

Simon Phillips, 13 July 2010

Have you ever wanted to get data from another form and display it on the current form?

Just the other day I was required to do this exact thing. After looking on the internet and also asking a few colleagues how this was possible I had acquired a few different ways. The first option was just a mammoth of JavaScript which could almost be mistaken for an English essay. The second option consisted of just a couple of lines

Option 1:

Below is what I call ‘The Mammoth’. This will do the required job if you can work out where to put the required field names etc. If you can work this out, I say well done to you.

function RetrieveAttribute(entityName, inputValue, outputProperty, inputProperty) {
   
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>" +
   
"            <q1:Attribute>" + outputProperty + "</q1:Attribute>" +
   
"          </q1:Attributes>" +
   
"        </q1:ColumnSet>" +
   
"        <q1:Distinct>false</q1:Distinct>" +
   
"        <q1:PageInfo>" +
   
"          <q1:PageNumber>1</q1:PageNumber>" +
   
"          <q1:Count>1</q1:Count>" +
   
"        </q1:PageInfo>" +
   
"        <q1:Criteria>" +
   
"          <q1:FilterOperator>And</q1:FilterOperator>" +
   
"          <q1:Conditions>" +
   
"            <q1:Condition>" +
   
"              <q1:AttributeName>" + inputProperty + "</q1:AttributeName>" +
   
"              <q1:Operator>Equal</q1:Operator>" +
   
"              <q1:Values>" +
   
"                <q1:Value xsi:type=\"xsd:string\">" + inputValue + "</q1:Value>" +
   
"              </q1:Values>" +
   
"            </q1:Condition>" +
   
"          </q1:Conditions>" +
   
"        </q1:Criteria>" +
   
"      </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);
   
xmlHttpRequest.send(xml);    // retrieve response and find attribute value
   
var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + outputProperty);
   
if (result == null)
       
return "";
   
else
       
return result.text;

Option 2:
Here is the code that I used to do the same thing with much fewer lines. The ‘Not So Mammoth’.

if (crmForm.all.<<attribute1>>.DataValue == null) { return; }

var sdk = new XrmDataContext(null, null);
var id = crmForm.all.<<Attribute1>>.DataValue[0].id;

var fetchXml = "<fetch mapping=\"logical\"><entity name=\"mag_identificationtype\"><all-attributes /><filter type=\"and\"><condition attribute=\"<<entity1>>\" operator=\"eq\" value=\"" + id + "\" /></filter></entity></fetch>";

var result = sdk.Fetch(fetchXml);
for
(var i = 0; i < result.length; i++) {
   
crmForm.all.
<<Attribute2>>.DataValue = parseFloat(result[i].attributes["<<attribute3>>"]);
}

Key for code:
Entity1 = the schema name of the entity you are pulling data from.
Attribute1 = the name of the attribute of the lookup to the entity.
Attribute2 = the name of the attribute of the field you wish to pull data from.
Attribute3 = the name to the attribute to place the data into.