Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Prevent Users to finish a BPF

Posted on by 873

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!

  • Suggested answer
    EBMRay Profile Picture
    EBMRay 873 on at
    RE: Prevent Users to finish a BPF

    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

  • Suggested answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: Prevent Users to finish a BPF

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans