Correcting Filtered Views and an Inaccurate “First ten” list

I ran into a situation at one of my customers while performing an upgrade from Dynamics CRM 2011 to 2015. On several of their entity forms, they have a filtered lookup in place where one value is filtered based on the value of another field. In this case it is an Account/Contact set of fields, which looks like this:

image

The idea is that the Primary Contact lookup will be filtered to show only contacts associated with the Potential Customer. This is accomplished with the following settings on the Primary Contact field:

image

How this works

Here is how this set of settings works:

1. You first check the Only show records where checkbox,

2. Then set the first field to use the Potential Customer (Accounts) (Quotes) value.

This setting is a little confusing, so here is my interpretation:

Potential Customer = the Potential Customer field.

(Accounts) = look for Accounts. This field can also contain Contacts.

(Quotes) = I am guessing this means the quotes entity.

3. The second setting instructs the lookup to look for Contacts where the Company Name field of the Contact is the same as the Potential Customer field.

The Problem

The problem is that it does not seem to be working the way I think it should. When you first click on the magnifying glass icon, you will get a list of 10 contacts, that seem to be unrelated to the Account specified in the Potential Customer field, as you can see below:

SNAGHTML27ff74f6

If you scroll to the bottom of the list, and click on Look up more records, you will see the standard lookup dialog which is properly filtered:

image

This confuses the user quite a bit and makes them do extra work. 

The Solution

At this point, I am under the impression that this is a bug. The list of 10 items needs to be the same as the items shown in the actual lookup dialog.  This means we have to resort to a little JavaScript.

Add this code to the JavaScript library associated with your entity:

function preFilterPrimaryContactLookup() { Xrm.Page.getControl("new_primarycontactid").addPreSearch(addPrimaryContacLookupFilter); } function addPrimaryContacLookupFilter() { if (Xrm.Page.getAttribute("customerid") == null) { return; } var customer = Xrm.Page.getAttribute("customerid").getValue(); if (customer == null) { return; } fetchXml = '<filter type="and">' + '<condition attribute="parentcustomerid" operator="eq" value="' +

customer[0].id + '" />' + '</filter>'; Xrm.Page.getControl("new_primarycontactid").addCustomFilter(fetchXml); }

The first method, preFilterPrimaryContactLookup, simply adds what we call a Pre-Search condition to the Primary Contact field. That condition actually calls the second method, addPrimaryContacLookupFilter, to actually do the work.

addPrimaryContacLookupFilter extracts the ID of the Potential Customer, then creates a FetchXml Filter Condition where the Contact entity is filtered based on where the Parent Account (parentcustomerid) of the Contact is the same as Potential Customer of the Quote (in this case).

We finish this exercise by adding the following line to the form’s OnLoad event:

    preFilterPrimaryContactLookup();

Conclusion

As I mentioned, I think this is a bug. I’ve seen it happen and not happen with different versions of Dynamics CRM 2015.

If you are trying to implement this technique, start with the standard configuration of the field, using the field properties in the form editor and if that does not work as you expect, add the JavaScript.