Change the display text in a lookup
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:
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.
Related Posts:
Read or set a value in a lookup
Hide / show new <record> button in a lookup
This function allows you to hide the “New” button from a lookup to stop users to create new records directly from within the lookup window.
//*********************************************************
gLookupDisplayNewButton = function(field, bShow){
/*
This function is used ti hide or show the New button in a lookuup
Params:
field = name of lookup field
bShow = integer (0 = hide, 1= show)
Returns: parameter
Example : gLookupDisplayNewButton('cm_instellingid', 0);
*/
var obj = document.getElementById(field);
if(obj != null){
obj.attachEvent("setadditionalparams",NewButton(obj, bShow));
}
//*********************************************************
function NewButton(field, bShow) {
return function(){
field.AddParam("ShowNewButton", bShow);
}
}
}
Read or set a value in a lookup
The following 2 functions enable you to read or set a value in a lookup. E.g. retrieve a text/GUID or set a default value to a lookup. Follow the instructions in each function below.
//****************************************************
gLookupGetValue = function (objLookup){
/*
This function is used read a value from lookup field
This function requires the following parameters:
objLookup: reference to the lookup object e.g. crmForm.all.productid
Returns: Array
[0] = returns lookup GUID
[1] = returns lookup text value
[2] = returns lookup entity type name (e.g. account)
[3] =//returns lookup entity type code (e.g. 1=account), 2=contact, etc)
Example:
var name = gLookupGetValue(crmForm.all.accountid)[1]
Author: GP
*/
var lookupItem = new Array;
var arr = new Array();
arr[0] = ""; //returns lookup GUID
arr[1] = ""; //returns lookup text value
arr[2] = ""; //returns lookup entity type name (e.g. account)
arr[3] = ""; //returns lookup entity type code (e.g. 1=account), 2=contact, etc)
// Get a reference to the lookup control on the form.
lookupItem = objLookup.DataValue;
// If there is data in the field, show it in a series of alerts.
if (lookupItem != null){
// Return the GUID of the lookup.
arr[0] = lookupItem[0].id;
// Return the text value of the lookup.
arr[1] = lookupItem[0].name;
// Return the entity type name of the lookup.
arr[2] = lookupItem[0].typename;
// Return the entity type code of the lookup.
arr[3] = lookupItem[0].type;
}
return arr;
}
//****************************************************
gLookupSetValue = function(objID, objText, objType){
/*
This function is used set a defaultvalue in a lookup field and requires the following parameters:
objID: ID of the record
objText: Text or Name of the record
objType: Entity code or Entity Name
Returns: lookup object
Designed by: Geron Profet
Example 1: retrieve based on entity type code
var defaultpricelevelname = 'Standard';
var defaultpricelevelid = gGetValue('pricelevelid','pricelevel','name',defaultpricelevelname)[2];
crmForm.all.pricelevelid.DataValue = gLookupSetValue(defaultpricelevelid, defaultpricelevelname, 1022);
Example 2: retrieve based on entity type name
crmForm.all.pricelevelid.DataValue = gLookupSetValue(defaultpricelevelid, defaultpricelevelname, 'pricelevel');
*/
// Create a lookupItem to store the values that you want to set to a target lookup control.
var lookupData = new Array();
var lookupItem= new Object();
//Set the id, name and typecode or typename properties to the object.
lookupItem.id = objID;
lookupItem.name = objText;
//if objType is not numeric, assume the entitytypename is passed
if (!isNaN(objType)){
lookupItem.type = objType;
} else {
lookupItem.typename = objType;
}
// Add the object to the array and return the lookupItem that you just created.
lookupData[0] = lookupItem;
return lookupData;
}
Filter a lookup in MS CRM
Below are 2 functions to filter a lookup.
- gFilterLookup() is used to filter a lookup based on another lookup.
- gFilterLookupValue() is used to filter a lookup based based on any value.
Both functions call: gReplaceSpecialChars() to replace any special chars or regular expressions.
//*****************************************
gFilterLookup = function(source, target) {
/*
This function is used to filter a lookup
Parameters: souce (e.g. a lookup field or text field)
target (e.g. the lookup you wish to filter)
Usage: gFilterLookup(crmForm.all.<sourcefilterfield>, crmForm.all.<targetlookupfield>);
The sourcefilterfield can either be a lookup of a text field
Returns: nothing
Author: GP
Updates: 20090525 GP: Added check to see if source lookup contains data
//NOTE: Make sure to add the sourcefilterfield (e.g. parentcustomerid) in the Entity's Lookup View (e.g. Contact Lookup View)
// For more info see: <a href="http://jianwang.blogspot.com/2008/05/mysterious-crm-lookup-ii.html">http://jianwang.blogspot.com/2008/05/mysterious-crm-lookup-ii.html</a>
*/
//check if objects exist
if (IsNull(source) || IsNull(target)) { return; }
//if source is a lookup field, use the name column of the lookupup array; else assume it is a text field
var name = (IsNull(source.DataValue)) ? '' : gReplaceSpecialChars(source.DataValue[0].name);
//add the filter conditions if source lookup contains data
if (!IsNull(source.DataValue)){
target.additionalparams = 'search=' + name;
}
else{
target.additionalparams = '';
}
}
//*****************************************
gFilterLookupValue = function(value, target) {
/*
This function is used to filter a lookup by a given value
Parameters: value (e.g. a value to be filtered on)
target (e.g. the lookup you wish to filter)
Returns: nothing
Calls: gReplaceSpecialChars()
Author: GP
Updates: 20090525 GP: Added check to see if value is filled and object exists
//NOTE: The column/attribute name needs to be defined as a search column of the lookup view,
// if it's not, the search paramater value will most likely not be found.
*/
//check if name if filled and lookup object exists
if (IsNull(name) || IsNull(target)) { return; }
//add the filter conditions
target.additionalparams = 'search=' + gReplaceSpecialChars(value);
}

