Sunday, June 14, 2020

addPreSearch functionality in dynamics 365

Filter a lookup using addPreSearch functionality


I'm going to show how to filter a lookup using addPreSearch functionality.

We have an option set field "new_region" which contains the different types of regions and based on those regions we are going to filter lookup to show only those country which comes under that region.

Follow the below code:


function AddFilterToCountryLookup(executionContext) {
    var formContext = executionContext.getFormContext();
    if (formContext.getControl("new_countryid") != null)
        formContext.getControl("new_countryid").addPreSearch(FilterCountryLookup);
}
 
function FilterCountryLookup(executionContext) {
    var formContext = executionContext.getFormContext();
    var region = formContext.getAttribute("new_region").getValue();
    var filter;
    if (region != null && region != "") {
        filter = "<filter type='and'><filter type='or'>";
        for (var i = 0; i < region.length; i++) {
            filter += "<condition attribute='new_region' operator='eq' value='" + region[i] + "'/>";
        }
        filter += "</filter></filter>";
    }
    else {
        filter = "<filter></filter>";
    }
    formContext.getControl("new_countryid").addCustomFilter(filter);

}

-----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment