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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Prevent Users to finish a BPF

(0) ShareShare
ReportReport
Posted on by 905

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!

I have the same question (0)
  • Suggested answer
    Nya Profile Picture
    29,060 on at

    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.

    (+) Prevent a user from going to new stage on a Business Process Flow - Microsoft Dynamics CRM Forum Community Forum

  • Suggested answer
    EBMRay Profile Picture
    905 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 93

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans