Friday, January 31, 2014

Dynamics CRM 2013 - Filter Lookup Dialogs

Dynamics 2013 includes some new methods to simply the process of filtering the results displayed in a lookup dialog. The concept is relatively straight forward. The PreSearch event is raised when the lookup dialog is initialised and by using an event handler we can call a function to add the required filter conditions when this event is raised.

In this example below we are going to filter the accounts lookup on the opportunity form to only display those where the opportunity contact is the primary contact on the account. 

We need to add a Javascript web resource with two functions to achieve this. 

function addParentAccountEventHandler() {

function addParentAccountFilter() {
    var id ="parentcontactid").getValue()[0].id;
    var filter = "<filter type='and'><condition attribute='primarycontactid' " +
              "operator='eq' value='" + id + "' /> </filter>";

The addParentAccountEventHandler function is called when the form is loaded to hook up the handler to the PreSearch event.

The addParentAccountFilter function runs when the events fires and it creates a filter condition to filter accounts only where the primarycontactid matches the opportunity contact id. The filter condition is then added to the lookup control by calling the addCustomFilter method. Only those accounts with a primary contact matching the opportunity contact are displayed. 

While this technique is relatively straight forward and easier to implement than the 2011 addCustomView technique it would have been great if this could have been achieved using business rules eliminating the need for any javascript,

No comments:

Post a Comment