Lookup Filtering using AddCustomFilter vs AddCustomView in Dynamics 365

Zoe Sands, 09 August 2018

Lookup Filtering Options and Gotchas in Dynamics 365

When it comes to lookup filters, sometimes the out of the box related Records Filtering doesn’t quite make the cut. You may want to filter by common values on the entity other than lookups or by more than one lookup. This is where you can utilize two functions in field controls to change filters or even the view that your lookup uses. These methods are AddCustomView and AddCustomFilter. What you use is dependent on what you need.

How to Use AddCustomView

If you want to filter by not only your primary record type but also a related record through a link-entity you will need to use AddCustomView. If you will not be using linked filtering, then you can use AddCustomFilter. In this example we are looking for accounts that have open cases and the cases have a primary contact equal to the one we have specified. We could have taken this contactId from another field on our source entity.

The parameters of this method are:
• A guid this must be unique among the ids of the other views on the account entity lookup
• The name of the entity
• The name the view will have
• The view fetchXml
• The view layout, this defines the fields that will appear in the view along with the space that they will be allowed. If you need help constructing this, you can grab it from an existing view in the customizations.xml file when you export a solution.
•    Whether or not this view will be the lookups default view


Example Filter:

Example Layout:


How to Use AddCustomFilter

This will add your filter to the filtering already applied to your default look up view, so if you’re going to use this you should ensure you either know you default filters or have them blank so you don’t get any surprises in your results.

The parameters of this method are:
• Filter criteria this does not contain the whole fetchXML definition. Instead it is only the filtering criteria.
• The entity that the view will look up to.

Example Fetch:


An interesting and annoying issue that I came across. See the example layoutXml below:


I have mistakenly used the wrong id field name as the result when an account is selected. When you select a record while this view is in use, it will appear to be selected but will not submit anything to the database when you save the record. If your lookup is required, you will be told to add a value to the lookup even though it looks like you have on the screen.