The following function enables you to change display value of a lookup in MS CRM. In the example below we have a custom entity to register multiple roles/relationships between accounts and contacts. Obviously, one contact can work for multiple accounts at any time or become a member of a society or association.

We have modified the case form so we can search for all relationships linked to the selected account (not just the primary ones) and  fill in the responsible contact. Since multiple roles can exist with the same name we decided to modify the name of the relationship lookup to display both the contact name and name of the role:

image

You can also use this function if for example in the account lookup you want to see account number instead of the account name.

Copy the function below in the onload event and use the example to call it.

//****************************************************
function gLookupChangeDisplayValue(fieldname, displayvalue){
 /*
 This function is used change the display value of a lookup
 This function requires the following parameters:
  fieldname :   name of the field
  displayvalue:  text to display in the lookup
 Returns: nothing
 Example:  Example: gLookupChangeDisplayValue('your field', 'text to display')
 Designed by: Geron Profet (www.crmxpg.nl)
 */
 var lookupData = new Array();
 var lookupItem= new Object();
 var lookup = document.getElementById(fieldname);

 if (lookup != null && lookup.DataValue != null){
  if(displayvalue != ''){
   lookupItem.id = lookup.DataValue[0].id;
   lookupItem.typename = lookup.DataValue[0].typename;
   lookupItem.name = displayvalue;
   lookupData[0] = lookupItem; 
   lookup.DataValue = lookupData;
  }
 }
}

TIP: alternatively you can also use gLookupSetValue(), see Related Posts.

Advertisements