Filter a Lookup on a Business Process Flow in Dynamics 365

Calum Jacobs, 30 June 2021

I was wanting to create a filtered look up on a BFP that showed cities based on the selected country that was a field within a form. Developing on from my colleague Sean’s blog, I came across an issue, where if the field was changed, the filter was not updating.

After investigating the issue, I solved this by removing the pre-search filter in the look up, updating the variable that the pre-search used, then re-applying the pre-search filter on the look up. This code is triggered when the field on the form is changed.

See the code below as to how I did this:

MAG.selectedCountryId = null;

MAG.filterBpfLookup = function (formContext) {

var eventCountry = formContext.getAttribute("mag_eventcountryid").getValue();

if (eventCountry != null) {

lookUpControl.removePreSearch(MAG.addCityFilter);

MAG.selectedCountryId = eventCountry[0].id;

lookUpControl.addPreSearch(MAG.addCityFilter);

}

}

Mag.addCityFilter = function (executionContext) {

var formContext = executionContext.getFormContext();

var filterBpf = "<filter type='and'>" +

"<condition attribute='mag_countryid' operator='eq' value='" + MAG.selectedCountryId + "' />" +

"</filter>";

formContext.getControl("header_process_mag_cityid").addCustomFilter(filterBpf);

}

If you have not read Sean’s blog, overall, the code is straight forward. I first get the ID of the country, from the country field on the form. Remove any filters previously set on the BPF Look Up (because if the country is changed later, the new filter does not override the old). Then I add the search filter to the BPF Look Up by calling Mag.addCityFilter, which is given the look up it is being applied to (header_process_mag_cityid).

The filter itself on the BPF Look Up is simple and can be retrieved from an Advanced Find. See below with the Advanced Find where I am getting Cities whose Country equals Australia:

clip_image001

Clicking Download Fetch XML returns the following:

image

After retrieving the fetch XML as you can see you only need to worry about filter and focus on everything below that ie:

image

You then take the attribute “mag_countryid” as the id/country is passed in, and you now have your filter for you code:

filterBpf = "<filter type='and'>" +

"<condition attribute='mag_countryid' operator='eq' value='" + MAG.selectedCountryId + "' />" +

"</filter>";