Avoid infinite loop when changing stage in the OnStageChange method
Hi everyone,
I have a function that fires when the opportunity stage change, checks for some requirements, and if the opportunity doesn't match those requirements it moves the stage back to what it was.
I'm using an attribute to save the current stage.
In the OnStageChange function, I set the stage back to the currentStage attribute like described above but that causes the OnStageChange method to fire again and I'm getting into an infinite loop.
Any ideas how to resolve it?
onStageChange: function() { var number = this.get("CurrentStage").Number; var ElementarySchool = this.get("UsrBlnElementarySchool"); var MiddleSchool = this.get("UsrBlnMiddleSchool"); var HighSchool = this.get("UsrBlnHighSchool"); if (number>=3 && ElementarySchool == false && MiddleSchool == false && HighSchool == false) { Terrasoft.showInformation(this.get("Resources.Strings.Stage3msg")); this.set("Stage", this.get("CurrentStage")); } this.set("CurrentStage", this.get("Stage")); }
Thanks,
Chani
Like
I made a few small changes to your code as you can see below,
Also I added a new boolean called "isManualChange" that you should add as a flag to your page, it should be false by default, and when the function first fires, it changes the value to True, then when the stage changes and the function is fired again, it changes the value back to False and exits the function without making any further changes
onStageChange: function() { var number = this.get("CurrentStage").Number; var ElementarySchool = this.get("UsrBlnElementarySchool"); var MiddleSchool = this.get("UsrBlnMiddleSchool"); var HighSchool = this.get("UsrBlnHighSchool"); var isManualChange = this.get("IsManualStageChange"); if (isManualChange) { // Reset the flag to false and exit the function this.set("IsManualStageChange", false); return; } if (number >= 3 && !ElementarySchool && !MiddleSchool && !HighSchool) { Terrasoft.showInformation(this.get("Resources.Strings.Stage3msg")); this.set("IsManualStageChange", true); this.set("Stage", this.get("CurrentStage")); } this.set("CurrentStage", this.get("Stage")); }
I made a few small changes to your code as you can see below,
Also I added a new boolean called "isManualChange" that you should add as a flag to your page, it should be false by default, and when the function first fires, it changes the value to True, then when the stage changes and the function is fired again, it changes the value back to False and exits the function without making any further changes
onStageChange: function() { var number = this.get("CurrentStage").Number; var ElementarySchool = this.get("UsrBlnElementarySchool"); var MiddleSchool = this.get("UsrBlnMiddleSchool"); var HighSchool = this.get("UsrBlnHighSchool"); var isManualChange = this.get("IsManualStageChange"); if (isManualChange) { // Reset the flag to false and exit the function this.set("IsManualStageChange", false); return; } if (number >= 3 && !ElementarySchool && !MiddleSchool && !HighSchool) { Terrasoft.showInformation(this.get("Resources.Strings.Stage3msg")); this.set("IsManualStageChange", true); this.set("Stage", this.get("CurrentStage")); } this.set("CurrentStage", this.get("Stage")); }