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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / addOnStageChange() cal...
Power Apps
Unanswered

addOnStageChange() called twice

(0) ShareShare
ReportReport
Posted on by 4

@

Hello,

 

I am writing javascript to call function on change of Business Process Flow. So in order to achieve above i have registered addOnStageChange() event as shown below:

 

//Function to call on-load
onLoad(executionContext: any, lockOrderFlagName: string, myAbilityFlagName: string, configRecordName: string) {

          let functionName: string = "onLoad";
          let formContext: any = null;
          try {

                  //Form Context
                  formContext = executionContext.getFormContext();

 

                 //Add Stage Change event
                formContext.data.process.addOnStageChange(() => objSalesOrder.onStageChange(executionContext,                                                                                                                              lockOrderFlagName, myAbilityFlagName, configRecordName));


              }
            catch (ex) {
               objSalesOrder.throwError(functionName, ex);
             }
}

Issue:

Every time i change the BPF Stage the handler i.e. objSalesOrder.onStageChange is executed twice.

 

Any help will be appreciated!

 

Thanks!

I have the same question (0)
  • Ram Prakash Duraisamy Profile Picture
    5,714 Super User 2026 Season 1 on at
  • Jason Nguyen Profile Picture
    Microsoft Employee on at

    It has been for long time since the question was raised, but I hope it will be helpful for anyone need it:

    - Don't use anonymous function for the addOnStageChange handler, and

    - Ensure you remove the named handler during onLoad before adding it to prevent it firing twice.
    So the script might look like this:


    function onStageChangeHandler(executionContext) {

    console.info("nextStageName");

    }

     

    function onLoad(executionContext) {

    var formContext = executionContext.getFormContext();

    formContext.data.process.removeOnStageChange (onStageChangeHandler);

    formContext.data.process.addOnStageChange (onStageChangeHandler);

    }

  • jenunn1 Profile Picture
    5 on at

    @Jason404, you have no idea how much that small script block have meant for me!!!!

    I re-visited my javascripts (probably 300 lines of javascript lines) and i have been struggling with this since forever since i have a lot of other scripts that requires formcontext.data.refresh(true) to save other events.

     

    This has made my "addOnPreStageChange", "AddOnStageChange" & "AddOnPreProcessChange" a nightmare, and i could not find anything useful on the "world wide web" until i stumbled upon your fix.

     

    Everything is MUCH smoother now and faster because there are not piling up 3-5 of the same events.

     

    THANK YOU ❤️

  • jenunn1 Profile Picture
    5 on at

    And yes, i created this user just to comment that you are a genious 😉

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 505

#2
WarrenBelz Profile Picture

WarrenBelz 502 Most Valuable Professional

#3
Haque Profile Picture

Haque 324

Last 30 days Overall leaderboard