Lookup a lookup on another entity using JScript

By Carlton Colter - Last updated: Thursday, October 15, 2009 - Save & Share - 2 Comments

Do you need the value of a lookup? I have written this bit of code to look a lookup value stored on another entity. Like finding the Customer lookup on an Incident (Case).


function getEntityLookup(id, entity, idfield, lookupfield, returntype) {
    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><soap :Body>" +
	"<fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
	"<fetchxml>" +
	" &lt;fetch mapping='logical' count='1'&gt;" +
	" &lt;entity name='" + entity + "'&gt;" +
	" &lt;attribute name='" + lookupfield + "' /&gt;" +
	" &lt;filter&gt;" +
	" &lt;condition attribute='" + idfield + "' operator='eq' value='" + id + "' /&gt;" +
	" &lt;/filter&gt;" +
	" &lt;/entity&gt;" +
	" &lt;/fetch&gt;" +
	"</fetchxml>" +
	"</fetch>" +
	"</soap>" +
	"";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;
    var resultSet = resultXml.text;
    resultSet.replace('&lt;', '< ');
    resultSet.replace('&gt;', '>');

    var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    oXmlDoc.async = false;
    oXmlDoc.loadXML(resultSet);

    var result = oXmlDoc.getElementsByTagName(lookupfield);
    if (returntype == 'value') return result[0].text;

    var lookupItem = new Object();
    for (var i = 0; i < result[0].attributes.length; i++) {
        var att = result[0].attributes[i];
        if (returntype == 'multiple' && att.name == "type") lookupItem.type = att.value;
        if (att.name == "name") lookupItem.name = att.value;
    }
    if (returntype != 'multiple') lookupItem.typename = returntype;

    lookupItem.id = result[0].text;

    return lookupItem;
}
Posted in Scripts • Tags: , , , , , , Top Of Page

2 Responses to “Lookup a lookup on another entity using JScript”

Comment from Todd
Time June 4, 2010 at 8:18 pm

Thanks for the post. Perhaps I am “thick”… but here is what I am trying to do. I an entity I created (called Goal) I have a lookup field for the Contact Name from the Contact entity. I want to put a Contact name in the Goal record. When the Contact name is selected, I want another field on the Goal entity (Contact phone) to automatically retrieve the Contact phone number from the Contact entity. Can your code do this?

Thanks

Comment from Carlton Colter
Time June 11, 2010 at 11:01 pm

That is easier than what this actually does. It looks up a lookup field on another entity, if you just want to lookup a field on a related entity, you could do that with this procedure now that I modified it. I modified the script to take value as a returntype so that you can use it to do what you are trying to do. You would call it with something like the line below. If it doesn’t work, let me know.

getEntityLookup(crmForm.all.contactlookup.DataValue[0].id, ‘contact’, ‘contactid’, ‘phonefieldname’,'value’);

Write a comment