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:
After:
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