Let's see how we could configure custom lookup filter in CRM. Consider the following scenario.
We have a field called General Practitioner on the Account entity. We would like to place this field as a lookup of contacts. But the lookup of contacts should be filtered based on the profession field of Contact entity. So the lookup would be a list of doctors whose profession='Doctor'.
The contact records without filtering:
Also the contact form is shown below.
The main steps for the lookup filtering are the following.
Step 1: Build FetchXML
Step 2: Build Grid Layout
Step 3: Attach the new view to the lookup
So lets do it.
Step 1: Build FetchXML
As the first step of filtering we need to have fetchxml. But we don't need to worry about creation of FetchXML. MS Dynamics CRM will do it for you. But we need to decide 2 things
1) View of the Lookup
2) Filtering criteria
So navigate to Advanced Find and search for the contact records whose profession = 'Doctor'
Then you would get the following result.
We have defined a custom view with limited number of fields as shown below.
If we go back to Advanced Find tab, we could see a button called Download Fetch XML
Download the fetchXML to any local folder. The fetch XML file is shown below.
Step 2 & 3 should done in directly in the Js file. So we should call js function at the form load of Account. For your convenience, I have pasted the contents of the js file with proper comments.
Note: Do not forget to reformat the contents of FetchXML file as shown in the code below.
Step2 is the process of building a layout for the Lookup which is commented in the code (Its always ideal to keep same fields in the view used with the fetchXML and lookup View(except Id field). Remember the step Decide view while doing fetchXML). Also the step3 which is attaching the filterd lookup view to the lookup field General Practitioner is commented in the code.
So here we go. As you could see only Doctors are listed in the lookup. The important point is that you could appy any filtering in the form of FetchXML. So I would say fetchXML is a cool feature in MS Dynamics CRM.