Custom Filtered Lookups in Microsoft CRM 4


Today we came across the great unsupported customization requests of Microsoft Dynamics CRM 3 which was a customized and filtered lookup box for Microsoft Dynamics CRM 4.0.

 Although lookup boxes are much easier to generate natively in Microsoft CRM 4.0 you just need to create a N:1 relationship to whatever entity you want your lookup box to pull information for.

For example on the opportunities entity you may want to not only have “potential customer”, but also have the ability to show a list of contacts to choose from that are specific to the selected customer. This is simply known as a filtered lookup, and here is an easy way to accomplish it.

After you have completed your many to one relationship (if one doesn’t already exist) on your customized entity you can easily with javascript filter the second lookup.

 You will first have to add the following onload event to your entities form.

document.FilterLookup = function(source, target)
{
    if (IsNull(source) || IsNull(target)) { return; }

    var name = IsNull(source.DataValue) ? ” : source.DataValue[0].name;

    target.additionalparams = ’search=’ + name;
}

Then on the onchange event of your attribute (in our case potential customer) you will have to trigger the filter function as in the example below, so that it will in fact also filter the second lookup field on the form as shown in the Custom Lookup Filter Image.

// If company is defined pre-filter lookup with customers
document.FilterLookup(crmForm.all.customerid, crmForm.all.sti_contact_opp_linkid);

 This also works best if you additionally copy the script to the onload of the form as well, this is so that if the user decides to change the existing contact to another contact it will still function as originally intended.

Leave a Reply

You must be logged in to post a comment.