I have written an event handler on change of stage in BPF in Opportunity Entity:
var checkForAccountApproved = function (executionContext) {
var formContext = executionContext.getFormContext();
formContext.data.process.addOnStageChange(function (stageContext) {
var stageName = stageContext.getEventArgs().getStage().getName().toString();
if (some conditions) {
formContext.data.process.movePrevious();
}
else {
currentActiveStage.setValue(stageName);
formContext.data.entity.save();
}
});
};
Basically, on clicking next, I need to check if some conditions are not satisfied, then only, the next stage should be active, else, the current stage should be active.
If the conditions are not satisfied, i.e. the stage change happens, I will store the value of the current active stage in a field. If I am explicitly calling save, the event handler is again triggered.
Why is it so?
Probably a late answer but ... don't use formContext.data.process.movePrevious() to cancel your stage change, as this will try to move the stage back and re-execute your pre-stage change ( this will probably keep looping till you get back to the first stage).. Instead use the below which cancels the stage change and stays at the current stage
executionContext.getEventArgs().preventDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With