Filter a lookup in MS CRM
January 1, 2008 by Geron Profet · Leave a Comment
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);
}

