web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Pre filtering CRM 2015 v 7.0.2.146

(0) ShareShare
ReportReport
Posted on by 149

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

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    if your parent lookup field doesn't have value, remove the addPreSearch. Always make sure there is a else statement .

    community.dynamics.com/.../170060

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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:

    msdn.microsoft.com/.../gg334266.aspx

  • Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    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. 

  • Verified answer
    bebeben10 Profile Picture
    149 on at

    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();

                   });

    }

  • Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    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.

  • bebeben10 Profile Picture
    149 on at

    Will definitely try this and update with the result

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans