Microsoft Dynamics 365

Dynamics 365 : Filtering and Fixing Lookup field Items

Atul Sharma

Next step from this article, where I did the basic introduction to lookup fields. Now I am moving to filter and fixing of views in a lookup control of a Dynamics CRM form.

Problem Statement – 

From my this View, I want only Leads, which are in Stage2, in my Lookup field. Also I don’t want the user to select any other view to search his record.

dynamics 365 fixed lookup view

but by default in my lookup field, I get all items like this –

See also  Dynamics 365 : Create a Lookup Field for Contact
dynamics 365 fixed lookup view

Solution –

Step# 1. Filtering the Lookup 

In your JavaScript web resource that is attached to your form, right this code –

dynamics 365 fixed lookup view

FetchXML explanation and variable used here are –

  1. What is my filter type?
  2. Field name in filter criteria. e.g. I want to filter on the basis of stage i.e. aks_stage and this is from my related entity.
  3. What is the value of that field? e.g. In my case, that field was drop down, so I have used its value. if it is some text or any numeric, then it has to be the exact value.
  4. What is my operator? e.g. – eq for equal, ne for not equal and so on…
  5. The closure of filter tag.
  6. aks_filteredLead – this is my field/attribute name where I have employed lookup fields. This control name belongs to my current form control.

If you have some more logic then you add event handler to addPreSearch()  then code will look like this –

dynamics 365 fixed lookup view
See also  Dynamics 365 : What is ‘Customer’ data type?

Step# 2. Fixing the Lookup views

Go to field properties -> Display -> Additional Properties

Now select OFF from View Selector and you can also assign a new view to fix search results.

dynamics 365 fixed lookup view

Result –

So after both of the above steps, I get this output and it is as expected.

dynamics 365 fixed lookup view

I hope this helps you, in your project scenario.