How to filter a lookup field in Business Process flow (BPF) in Dynamics CRM

How to filter a lookup field in Business Process flow (BPF) in Dynamics CRM

 

Unfortunately there is no out of the box way to apply “related records filter’ on a lookup field in the Business process flow.

Butt from Dynamics CRM 2013 it is possible to access the fields in BPF via JavaScript and achieve similar results. When the BPF is added to the form, additional controls also get added for the BPF fields, like: header_process_<attribute name>. Using these field names we can write JavaScript to filer a lookup

Here is an example that I worked on.

dynamics_crm_filter_lookup

I had to filter the ‘UB Agent’ based on the value selected on ‘UB Group’.

The javascript code is given below:

function filterUBAgentLookup() {

    //Check if the control exists on the form

    if (Xrm.Page.getControl(“header_process_new_ubagentid”) != null) {

        // add the event handler for PreSearch Event

        Xrm.Page.getControl(“header_process_new_ubagentid”).addPreSearch(addUBAgentFilter);

    }

}

function addUBAgentFilter() {

    var ubgroupId = null;

    var ubgroupLookup;

    var fetchQuery;

    try {

        //Check if control exist on form

        if (Xrm.Page.getControl(“header_process_new_ubgroupid”) != null && Xrm.Page.getControl(“header_process_new_ubgroupid”).getAttribute().getValue() != null) {

            //Get ubgroup lookup value

            ubgroupLookup = Xrm.Page.getControl(“header_process_new_ubgroupid”).getAttribute().getValue();

            //Get the ubgroup id

            ubgroupId = ubgroupLookup[0].id;

        }

        //Build fetch

        if (ubgroupId != null || ubgroupId != undefined) {

            fetchQuery = “<filter type=’and’>” +

            “<condition attribute=’statecode’ operator=’eq’ value=’0′ />” +

            “<condition attribute=’new_ubgroupid’ operator=’eq’ value='” + ubgroupId + “‘ />” +

            “</filter>”;

            //add custom filter

            Xrm.Page.getControl(“header_process_new_ubagentid”).addCustomFilter(fetchQuery);

        }

    } catch (e) {

        Xrm.Utility.alertDialog(“addFilter Error: ” + (e.description || e.message));

    }

}

Apply the code on OnLoad event on the form. Also apply it on Change event of UB Group.

1,947 total views, 2 views today

About Author

Ricky Saini

Ricky Saini
Dynamics CRM, SharePoint, Office 365, Windows Azure and .Net Architect/Consultant/Freelancer in Australia ( Melbourne, Sydney and Canberra)

Leave a Comment