Hi All,
I am aware that there is an out of the box functionality to pre filter a lookup field based on another lookup field. However, that out of the functionality works only in the form not in the Business Process Flow, isn't? So I have created a JavaScript functionality that adds addPreSearch() function to the lookup field I would like to be pre-filtered. This is working fine when I populated the parent lookup field then populate child lookup field. But, when I deleted the value from both lookup fields then populated them again, on my child lookup field, it will say "No records found". This does not work in the form as well. Below is the code for my pre-filter.
if (Xrm.Page.getControl("header_process_new_ratetype") != null) {
Xrm.Page.getControl("header_process_new_ratetype").addPreSearch(function () {
fetchXml = "<filter><condition attribute='new_ratecategory' operator='eq' value='" + rateCategory[0].id + "' /></filter>";
Xrm.Page.getControl("header_process_new_ratetype").addCustomFilter(fetchXml);
});
}
Am I missing something here?
Thanks in advance.
Regards,
Marvin
*This post is locked for comments
Will definitely try this and update with the result
Hi, try my function. It's better because we only regis the function 1 time only. When lookup triggered searching, the function will be invoke automatically without you need to delete the presearch.
Hi All,
Thanks for your inputs.
Adding Xrm.Page.getControl(arg).removePreSearch(handler) in the code fixed the issue.
Though in a different way as you have suggested.
Below is the exact block of code I have implemented and worked.
function addPreSearchFilter() {
// fetchXML = "";
// Xrm.Page.getControl(arg).addCustomFilter(fetchXml);
}
function mainFunction () {
Xrm.Page.getControl(arg).removePreSearch(function () {
addPreSearchFilter();
});
Xrm.Page.getControl(arg).addPreSearch(function () {
addPreSearchFilter();
});
}
Hi. For PreSearch function I'm prefer using IFFE(Immediately Invoked Function Expression):
This is example how to create function with IFFE style:
function AddPresearch(){ var isRun; var fnSearch = function(){ var category = Xrm.Page.getAttribute("ratecategory").getValue(); if(category==null) return; var fetchXml = "<filter><condition attribute='new_ratecategory' operator='eq' value='" + category[0].id + "' /></filter>"; Xrm.Page.getControl("header_process_new_ratetype").addCustomFilter(fetchXml); }; return function(){ if(!isRun && Xrm.Page.getControl("header_process_new_ratetype") != null){ Xrm.Page.getControl("header_process_new_ratetype").addPreSearch(fnSearch); isRun = true; } }(); }
Just register the function in onload / bpf load.
Hi Marvin,
As also mentioned above, you need to remove the custom filter if the condition fails.
Xrm.Page.getControl(arg).removePreSearch(handler)
Take a look here:
Hi,
if your parent lookup field doesn't have value, remove the addPreSearch. Always make sure there is a else statement .
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
52
Victor Onyebuchi
6