Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Business Process flow - change lookup filter base on a parameter on Change events

(0) ShareShare
ReportReport
Posted on by 1,530

Hi,

I have a business process flow that seem to only work if the field (Reason) that I need to do the FILTER lookup on exist in the first stage.

FirstStage.JPG

When I remove the Reason field from the first stage to the second stage the FILTER no longer works.

0640.Capture.JPG

The below logic is working on the first stage.

if (Xrm.Page.getControl("header_process_new_reasonid") != null) {
 
    Xrm.Page.getControl("header_process_new_reasonid").removePreSearch(addLookupFilter);
 
    Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);
}

I thought if I added the below logic to trigger on a stage change using OnLoad or OnSave still no luck.

Xrm.Page.data.process.addOnStageChange(stageChange);
Xrm.Page.data.process.addOnStageSelected(stageChange);

function stageChange() {
    Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);
}

Does javascript logic only work on the first stage.

Any ideal on what I am doing wrong.

Thanks

*This post is locked for comments

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Thanks Ravi,

    I will rebuild everything and try again.

    Thanks very very much for taking the time to look into this.

    I really appreciate you doing this.

    Thanks again.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi,

    I have used all. "Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);" is the actual javascript to filter the records whereas "addOnStageChange" & "addOnStageSelected" are the methods to attach the method when the stage is selected & stage is changed.

    Here is what I have -

    ============================

    function OpportunityOnLoad() {

       addLookupFilter();

       Xrm.Page.data.process.addOnStageSelected(addLookupFilter);

       Xrm.Page.data.process.addOnStageChange(addLookupFilter);

    }

    function addLookupFilter() {

       var account = Xrm.Page.getAttribute("parentaccountid").getValue();

       if (account == null) {

           if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {            

               Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

           }

       }

       else {

           if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {

               Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

           }

       }

    }

    function filter() {

       var fetchXml = "<filter type='and'> <condition attribute='firstname' operator='like' value='s%' /></filter>";

       Xrm.Page.getControl("header_process_parentcontactid").addCustomFilter(fetchXml);

    }

    ============================

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi Ravi,

    Were you using

    Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);

    or

    Xrm.Page.data.process.addOnStageChange(stageChange);

    or

    Xrm.Page.data.process.addOnStageSelected(stageChange);

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi,

    I tried the same implementation (though for oob entity and field) and this seem to work as expected. i.e. On opportunity I filter the Contact lookup which is added to the OOB BPF. The contact lookup filtered bsed on my condition. Then I update the BPG, moved the field to second stage. The filter work as expected there.

    Then I added the field on stage 1 as well as stage 2. The filter stopped on stage 2. This is because when you add the control more than once, the id changes.

    Also, I have noticed several time that if the filter applies on load and if you are applying the filter again then sometime it doesn't work. In these scenarios, we need to remove the previously added filter. docs.microsoft.com/.../removepresearch

    Hope this helps.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Great!!!  Thanks very much Ravi.  This would be great if you can get this working.

    I can only copy and paste that code JS library .  Please let me know if you want me to do that.

    Thanks again for all your help.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Sorry I meant the full code in your library. I am suspecting there is some syntax error which is why its not working.

    Its okay if you are not able to share. I will try the same thing on my trial and let you know how it goes.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi Ravi,

    Here's my js library and my form properties.

    formSetup.JPG

    formSetup.JPG

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi,

    By any chance, can you share your complete JS library?

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi Goutam,

    I removed a lot of the code and made if very basic.  I am still having the same problem with the addLookupFilter not firing on second stage.  However the form "Reason" does work with the logic.

    Could there be a problem with adding 2 hook events?

    OnFormLoad
    
    {
    
    Xrm.Page.data.process.addOnStageSelected(stageChange);
    Xrm.Page.data.process.addOnStageChange(stageChange);
    }
    
    

    //Stage Change
    function stageChange() {
        try {
    
            if (Xrm.Page.getControl("new_reasonid") != null) {
                Xrm.Page.getControl("new_reasonid").addPreSearch(
                function () {
                    addLookupFilter();
                });
            }
    
            if (Xrm.Page.getControl("header_process_new_reasonid") != null && Xrm.Page.getControl("header_process_new_reasonid") != undefined) {
                console.log("help me");
                Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(
                function () {
                    addLookupFilter();
                });
            }
        } catch (e) {
            //throw error
            throw new Error(e.message);
        }
    }

    //This function is called from the field to do the filterLookup
    function addLookupFilter() {
        alert("Is this working after stage change");
    
    }
  • Suggested answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Business Process flow - change lookup filter base on a parameter on Change events

    Hi Thompson ,

    Try to add an alert beginning of  addLookupFilter() function check whether you are getting alert of not .

    Also if you are not getting any alert in my earlier post "  alert("Filter apply successfully in BPF field");" which means BPF field is getting null and thats why its not adding filter criteria.

    The code is correct and I don't see any error in the code .

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans