Home > Extensions > Web Resources > Scripts > Lookup a lookup on another entity using JScript

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).

  • ID: The unique id value of the entity’s object
  • ENTITY: The name of the entity to lookup
  • IDFIELD: The entity’s primary id field name, like accountid, incidentid, etc.
  • LOOKUPFIELD: The name of the field you want to lookup, like customerid
  • RETURNTYPE: The name of the entity’s return type, like contact or account. If there are multiple possible, then specify “multiple” and it will pass back the type instead of type name. If you specify “value” it will return just the value.

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;
}

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

Comments (8)
  1. 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

  2. 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’);

  3. How can I get this to work in CRM 2011? Can you email me with a response?

  4. Hi Carl,

    I am interested in how to do this in CRM online 2011 as well…..or will this code work?

    • This code should work the way it is with CRM 2011 since the 2007 web services are still available. You could also probably use the REST SDK or a SOAP call to look it up.

  5. does it work with sub-grid in place of lookupfield ???

    • Damien,

      Are you trying to get the value of a lookup in a sub-grid, or are you trying to get the value from the lookup field’s entity, etc? If you could explain in more detail what you are trying to do, I could try and help you accomplish it.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.