Announcements
Hello everyone,
I am trying to prevent users to move next or previous or finish a business process flow by injecting a custom JS script in the form.
I followed an amazing article over here that shows clearly how to do this.
https://debajmecrm.com/stop-users-for-performing-abandon-finish-operations-on-a-business-process-flow-in-dynamics-365-using-client-api/
The code that I am using is the below:
function navigationContolLoad(e) { debugger; var fc = e.getFormContext(); // use the below code to remove a registered event handler. //fc.data.process.removeOnPreStageChange(Acc.formEvents.handlePreStage); fc.data.process.addOnPreStageChange(handlePreStage); //to stop users to finish BPF fc.data.process.addOnPreProcessStatusChange(handlePreStage); } function handlePreStage(e) { debugger; // get the event arguments var bpfArgs = e.getEventArgs(); var fc = e.getFormContext(); var isTrd = isTrader(fc); var isComp = isCompliance(fc); var currentStageName = fc.data.process.getActiveStage().getName(); var allowNext = true; var allowPrevious = true; var allowFinish = true; if(isTrd){ allowPrevious = false; allowFinish = false; if(currentStageName == "Test1" || currentStageName == "Test2"){ allowNext = true; }else{ allowNext = false; allowFinish = false; } } if(isComp){ allowNext = true; allowPrevious = true; allowFinish = true; } if (bpfArgs.getDirection() === "Previous" && (allowPrevious==false)) // back stage movement is not allowed; You can stop it depending on custom business logic as well { bpfArgs.preventDefault(); var alertStrings = { confirmButtonLabel: "OK", text: "Back stage movement is not allowed", title: "Business Validation" }; var alertOptions = { height: 120, width: 260 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions); return; } if (bpfArgs.getDirection() === "Next" && (allowNext==false)) { // stop the stage movement bpfArgs.preventDefault(); alertStrings = { confirmButtonLabel: "OK", text: "Next stage movement is not allowed", title: "Business Validation" }; alertOptions = { height: 120, width: 260 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions); return; } if (bpfArgs.getDirection() === "Finish" && allowFinish == false) { bpfArgs.preventDefault(); alertStrings = { confirmButtonLabel: "OK", text: "Finish stage movement is not allowed", title: "Business Validation" }; alertOptions = { height: 120, width: 260 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions); return; } }
I am still struggling to prevent the user from clicking on the finish button and I am pretty sure this is because I am not calling the "addOnPreProcessStatusChange" in the third condition.
Can someone check my code and advice what I am doing wrong and provide a solution?
Any help is highly appreciated.
Thank you!
Hello,
Thank you for your response.
I have to use JS because I want to prevent the users to go next or back based on the user security role.
So in order to do that I should use JS.
I checked the mentioned solution, However, after I did my investigation I found that you can't check for Direction = Finish in BPF and I missed providing in my condition getEventArgs() function.
Now the issue has been resolved.
Regards,
Julien
Hi,
Do you have to use JavaScript to implement this requirement?
If not, you can refer to the answer of the following issue, which is a fairly simple and effective solution to stop the process.
André Arnaud de Cal...
294,095
Super User 2025 Season 1
Martin Dráb
232,866
Most Valuable Professional
nmaenpaa
101,158
Moderator