How to display related entity field in a CRM Lookup instead of the primary entity field?


As you all know a CRM lookup only displays its related entity primary field. Although this can not be changed using existing customizations; in most cases that suffices.

However, there are occasions where you want to display another information in order to avoid opening the related entity form.

So I created a generic JavaScript function to display another attibute inside CRM lookup.



On a contact form you want to display the accountnumber field, inside the parent customer lookup, instead of primary field of the account entity.





Step 1

Go on the form where the lookup is displayed:

On the onload event, add this line of code and change LookupSchemaName by the schema name of your lookup.


Step 2

On the onchange event of the lookup, Copy the following code (inside the table):

  • fieldToDisplay = the name of the attribute that you want to display in the lookup. Take the schema name of the attribute in the linked entity.

  • fieldToDisplayIsText:

    • true if you want to display a nvarchar field.

    • false if you want to display a picklist or a lookup field.

  • organizationName = name of your organization (without spaces).

var fieldToDisplay = 'accountnumber';
var fieldToDisplayIsText = true;
var organizationName = 'MyOrganizationName';

var lookupData = new Array();
var lookupItem= new Object();
var lookup = event.srcElement.DataValue;

if (typeof(lookup) != 'undefined' && lookup != null && lookup[0] != null)
var myValue = GetAttributeValueFromID(lookup[0].typename,lookup[0].id,fieldToDisplay,fieldToDisplayIsText);

if(myValue != '')
{ = lookup[0].id;
   lookupItem.typename = lookup[0].typename; = myValue;
   lookupData[0] = lookupItem; 
   crmForm.all[].DataValue = lookupData;


function GetAttributeValueFromID(sEntityName, sGUID, sAttributeName, isTextField)
var xml = "" +

"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\">" +

GenerateAuthenticationHeader() +

"  <soap:Body>" +

"    <Execute xmlns=\"\">" +

"      <Request xsi:type=\"RetrieveRequest\" ReturnDynamicEntities=\"false\">" +

"        <Target xsi:type=\"TargetRetrieveDynamic\">" +

"          <EntityName>" + sEntityName + "</EntityName>" +

"          <EntityId>" + sGUID + "</EntityId>" +

"        </Target>" +

"        <ColumnSet xmlns:q1=\"\" xsi:type=\"q1:ColumnSet\">" +

"          <q1:Attributes>" +

"            <q1:Attribute>" + sAttributeName + "</q1:Attribute>" +

"          </q1:Attributes>" +

"        </ColumnSet>" +

"      </Request>" +

"    </Execute>" +

"  </soap:Body>" +

"</soap:Envelope>" +


var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);


xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xmlHttpRequest.setRequestHeader("Content-Length", xml.length);


var result = null;


result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName).text;




result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName).getAttribute('name');

                        if (result == null)
                                    return '';
                        return result;

Step 3

Save and publish the entity



Comments (9)

  1. As you all know a CRM lookup only displays its related entity primary field. Although this can not be

  2. apirani says:


    Is it possible to place a lookup on custom ASPX page. I have a need where in we are developing a custom form which requires a lookup field to be placed on it.


  3. stella7113 says:

    I am wondering, is it possible to have a lookup field in a form from the same entity?

    For example, in the contacts form, I want to have a lookup for an assistant that is also a record in the contact form

    How can this be done?

    THank you


  4. tanguy92 says:

    Hi Pierre Adrien,

    Nice post…

    If the point is a prerequisite from the customer, I prefer the plugin method where you modify the result of the query…

  5. Links of the week #79 (a.k.a. long overdue edition)

  6. Zunanji viri says:

    Development How to choose between ASP.NET MVC and Web Forms The Microsoft Dynamics CRM Developer Toolkit

  7. MSDN Archive says:

    Nice post. Although, GenerateAuthenticationHeader [] can be used to generate headers.




  8. says:

    Hi, I have problem with your script:

    I have changed it little bit 🙂 I would like to change different field and not the lookup itself (grab a value from related entity, fill it to a field and not change the lookup) It works, but when I change the lookup it fills the text field I wanted but in the lookup field is nothing left but the icon. (there is no text) Could you help me?




    alert (result);

    instead of


    return result;

Skip to main content