May 20, 2018

Filtered Lookup for Customer type fields

I shared a code snipped for a filtered lookup here. When it comes to Customer Type lookup, it is bit more complex than a usual Lookup. Let's have a look;

Consider parentcustomerid field of Contact. Though it is a lookup, it is a Customer type lookup. Usually a lookup refers another Entity Type; Customer Type refers two types, namely Account and Contact Entities. If you Open this field’s customisation, you will see below details on two different relationships.


Now suppose I need to filter this loop to show only Accounts of only one Business Types (say BusinessTypecode = 1 for example). We may think of implementing below code.

AccountFilter: function () {
var accFilter = 
"<filter type='and'>
   <condition attribute='businesstypecode' operator='eq' value='1' />
</filter>";
Xrm.Page.getControl("parentcustomerid").addCustomFilter(accFilter,"accdount");
}

//Calling
Xrm.Page.getControl("parentcustomerid").addPreSearch(AccountFilter);

Then you will realise, this is half the solution. Though this filters Accounts correctly, Contacts are Not filtered at all. In fact, we need to filter-out all the contacts as below.

ContactFilter: function () {
var conFilter = 
"<filter type='and'>
   <condition attribute='lastname' operator='eq' value='' />
</filter>";
Xrm.Page.getControl("parentcustomerid").addCustomFilter(conFilter, "contact");
}

//Calling
Xrm.Page.getControl("parentcustomerid").addPreSearch(ContactFilter);

Now we have tried to filter Contacts with null last name. Since Last name is mandatory there are no contacts with null Last name, resulting None of the Contacts are shown for Lookup.

This solves the problem. When you need to filter RegardingObject of Activities, you may need to implement many filtering criteria just like this.

One other thing to keep in mind is, same way we use addPreSearch(), we also can removePreSearch() extension as necessary.

2 comments:

  1. Hello, i have this problem that the field shows Contacts and Company Names instead of just Company. i'm not familiar with the snipping tool or Javascript. can you please tell me how to access the code snipping tool?

    ReplyDelete