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; } }
The above code is working and preventing the user to move next or previous between stages but he's allowed to click on the "Finish Button" in the last stage of the business process flow.
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!