Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

JavaScript to confirm before BPF movement not working

(0) ShareShare
ReportReport
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

News and Announcements

Announcing Category Subscriptions!

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans