Recently I published a post describing how to access the various components of a business process flow, including the currently active process flow, details about the stages in the process and also the steps within the stage.

Here I will build upon that post and look at what actions can be performed on a business process flow.

Get active Stage

If you need to get the name or Id of the current stage, follow code below;

var activeStage = Xrm.Page.data.process.getActiveStage();

alert(activeStage.getId());

alert(activeStage.getName());

To hide the active business Process Flow

Xrm.Page.ui.process.setVisible(false); // ** Or true

To expand or collapse the active business process flow

Xrm.Page.ui.process.setDisplayState("collapsed"); // *** Or expanded

Move to next stage in the business process

You can use the moveNext method to navigate to the next stage in the business process. As shown below;

var activeProcess = Xrm.Page.data.process.getActiveProcess;
if (activeProcess != null) {
  Xrm.Page.data.process.moveNext(function (result) {
    if (result == "success") {
      // ** Code for success goes here
      alert("Worked");
    } else {
      // ** Code for failure goes here
      alert("Had an Issue");
    };
  });
};

Move to previous stage in the business process

You can use the movePrevious method to navigate to the next stage in the business process. As shown below;

var activeProcess = Xrm.Page.data.process.getActiveProcess;
if (activeProcess != null) {
  Xrm.Page.data.process.movePrevious(function (result) {
    if (result == "success") {
      // ** Code for success goes here
      alert("Worked");
    } else {
      // ** Code for failure goes here
      alert("Had an Issue");
    };
  });
};

Add a function triggered whenever user selects a stage

You can define an “On Stage Selected” function that will be triggered whenever a stage is clicked by the user.

//adding a named function on stage selection
Xrm.Page.data.process.addOnStageSelected(StageSelected);

function StageSelected() {
  alert("Stage selected!");
}

OR you could use the following syntax

Xrm.Page.data.process.addOnStageSelected(function () {
  Alert("Stage selected");
});

Set Active Stage

You can set the active stage, by using the ID of the stage.

Xrm.Page.data.process.setActiveStage("15322A8F-67B8-47FB-8763-13A28686C29D" (result) {
if(result == "success") {
     alert("Success");
 } else {
     alert("Invalid");
 }
});

Happy coding!