Skip to main content

Notifications

Microsoft Dynamics CRM forum
Unanswered

JavaScript to confirm before BPF movement not working

Posted on by 10

Hi Experts,

Need help on below JavaScript, everything working fine except, when user selects the next stage, Confirm Dialog prompt for confirmation, but when user click on OK button in the dialog box, this action does not move to next stage. Simply its close the dialog box. if anyone can help me on this?

function onLoad(executionContext) {
    var formContext = executionContext.getFormContext();
    var process = formContext.data.process;        

    //handles changes to the BPF before they actually occur
    process.addOnPreStageChange(myProcessStateOnPreChange);
}

function myProcessStateOnPreChange(executionContext) {

    var formContext = executionContext.getFormContext();
    var process = formContext.data.process;
    var eventArgs = executionContext.getEventArgs();

    var currentStageId = process.getActiveStage().getId();
    var nextStageId = process.getSelectedStage().getId();

    //dont allow to go back using the set active button
    if (currentStageId != nextStageId) {
        eventArgs.preventDefault();
        Xrm.Utility.alertDialog("You cannot set completed stage as Active.")
        return;
    }        
         
    if (eventArgs._direction === 1) //backwards
    {            
        //logic based upon the BPF going to the previous Stage.
        eventArgs.preventDefault();
          var alertStrings = { cancelButtonLabel: "Cancel", confirmButtonLabel: "OK", text: "Backward stage movement is not allowed", title: "Warning" };
          var alertOptions = { height: 120, width: 260 };
          Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
        return;
    }

    //otherwise forward
    //logic based upon the BPF going to the next Stage.
    else
    {   eventArgs.preventDefault();
        var confirmStrings = {
            cancelButtonLabel: "Cancel",
            confirmButtonLabel: "OK",
            text: "Are you sure you want to move forward in the BPF?",
            title: "Confirm BPF Movement"
        };
        var confirmOptions = { height: 120, width: 260 };
        Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function(success) {
            if (success.confirmed) {
                formContext.data.process.moveNext();
                console.log("BPF movement confirmed");
            } else
            {
                console.log("BPF movement cancelled");
            }
        });
    }
    //Xrm.Utility.alertDialog("Warning: You cannot undo this movement.")
  }
 

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans