Skip to main content

Notifications

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 130

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

  • bebeben10 Profile Picture
    130 on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    Will definitely try this and update with the result

  • Temmy Wahyu Raharjo Profile Picture
    2,914 on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    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.

  • Verified answer
    bebeben10 Profile Picture
    130 on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    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,914 on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    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. 

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    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

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Pre filtering CRM 2015 v 7.0.2.146

    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

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans