Delen via


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

Overview

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.

Sample

 

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

Before:

image

After:

image 

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.

crmForm.all.LookupSchemaName.FireOnChange();

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 != '') {

  lookupItem.id = lookup[0].id;    lookupItem.typename = lookup[0].typename;    lookupItem.name = myValue;    lookupData[0] = lookupItem;     crmForm.all[event.srcElement.id].DataValue = lookupData; }

}

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

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

"<soap:Envelope xmlns:soap=\"https://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"https://www.w3.org/2001/XMLSchema\">" +

GenerateAuthenticationHeader() +

"  <soap:Body>" +

"    <Execute xmlns=\"https://schemas.microsoft.com/crm/2007/WebServices\">" +

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

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

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

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

"        </Target>" +

"        <ColumnSet xmlns:q1=\"https://schemas.microsoft.com/crm/2006/Query\" 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("SOAPAction","https://schemas.microsoft.com/crm/2007/WebServices/Execute");

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

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

xmlHttpRequest.send(xml);

var result = null;

if(isTextField){

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

}

else

{

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

}                         if (result == null)                         {                                     return ''; }                         else                         return result;             }

Step 3

Save and publish the entity

 

PAF

Comments

  • Anonymous
    April 21, 2009
    PingBack from http://asp-net-hosting.simplynetdev.com/how-to-display-related-entity-field-in-a-crm-lookup-instead-of-the-primary-entity-field/

  • Anonymous
    April 27, 2009
    As you all know a CRM lookup only displays its related entity primary field. Although this can not be

  • Anonymous
    April 27, 2009
    Hi, 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. Thanks.

  • Anonymous
    April 29, 2009
    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 Stella

  • Anonymous
    April 30, 2009
    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...

  • Anonymous
    May 07, 2009
    The comment has been removed

  • Anonymous
    May 07, 2009
    Development How to choose between ASP.NET MVC and Web Forms The Microsoft Dynamics CRM Developer Toolkit

  • Anonymous
    May 12, 2009
    Nice post. Although, GenerateAuthenticationHeader [http://technet.microsoft.com/en-us/library/cc905753.aspx] can be used to generate headers. HTH,


Chinmay

  • Anonymous
    July 12, 2009
    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? example: ...... else alert (result); instead of else return result;