With Microsoft Dynamics CRM 2015, the Xrm.Page.data.process namespace provides events, methods, and objects to interact with the business process flow data in a form.
This sample demonstrates how to switch to different stage using the new Xrm.Page.data.process methods.
Followings are the API used in the sample code:
getActiveProcess() – retrieve information about the active process
setActiveProcess()- Set different process as the active process.
getActiveStage()- retrieve information about the active stage
setActiveStage() – set a different stage as the active stage.
getActivePath() – get a collection of stages currently in the active path with methods to interact with the stages displayed in the business process flow control.
addOnStageChange(),removeOnStageChange() – add or remove event handlers for the business process flow control.
addOnStageSelected() – add a function as an event handler for the OnStageSelected event so that it will be called when a business process flow stage is selected.
removeOnStageSelected() – remove a function as an event handler for the OnStageSelected event.
moveNext()- move to the next stage
movePrevious()- Moves to the previous stage.
stageObj.getId() – Returns the unique identifier of the stage
stageObj.getName() – Returns the name of the stage
getEnabledProcesses() – retrieve the enabled business process flows that the user can switch to for an entity
Followings are the Javascript to demonstrate how to switch the process stage:
//move to new stage by stage name
function moveToStage(newStageName){
//get active stage
var activeStage = Xrm.Page.data.process.getActiveStage();
var activeStageName = activeStage.getName();
//get a collection of stages currently in the active path
var activePathCollection = Xrm.Page.data.process.getActivePath();
//Enumerate the stages
activePathCollection.forEach(function (stage, n) {
var name = stage.getName();
//search for specific stage by name
if( (newStageName == name) && (newStageName != activeStageName) ) {
//move to the new stage
Xrm.Page.data.process.setActiveStage(stage.getId() , onSetActiveStage);
}
})
}
//This callback function is passed the result to indicate whether the setActiveStage operation succeeded.
function onSetActiveStage(returnStatus){
switch (returnStatus) {
case “success”:
break;
case “crossEntity”:
alert(“crossEntity!”);
break;
case “unreachable”:
alert(“unreachable stage”);
break;
case “invalid”:
alert(“invalid stage”);
break;
}
}
//Progresses to the next stage
function moveNext(){
Xrm.Page.data.process.moveNext(onMoveNext);
}
//This callback function is passed the result to indicate whether the moveNext operation succeeded.
function onMoveNext(returnStatus){
switch (returnStatus) {
case “success”:
break;
case “crossEntity”:
alert(“crossEntity!”);
break;
case “end”:
alert(“Last stage”);
break;
case “invalid”:
alert(“invalid stage”);
break;
}
}
//Progresses to the next stage
function movePrevious(){
Xrm.Page.data.process.movePrevious(onMovePrevious);
}
//This callback function is passed the result to indicate whether the movePrevious operation succeeded.
function onMovePrevious(returnStatus){
switch (returnStatus) {
case “success”:
break;
case “crossEntity”:
alert(“crossEntity!”);
break;
case “beginning”:
alert(“Stage beginning”);
break;
case “invalid”:
alert(“invalid stage”);
break;
}
}
// Methods to manage event handlers
// on form load
function registerProcessEvent(){
//add OnStageChange event handlers for the business process flow control.
Xrm.Page.data.process.addOnStageChange(onStageChange);
//add OnStageSelected event handlers for the business process flow control.
Xrm.Page.data.process.addOnStageSelected(onStageSelected);
//show current process
retrieveActiveProcess();
//show current stage
retrieveActiveStage()
}
//Retrieve active process
function retrieveActiveProcess(){
//Get active process
var activeProcess = Xrm.Page.data.process.getActiveProcess();
var processName = activeProcess.getName();
var processID = activeProcess.getId();
alert(“Acitve process is ” + processName + ” ID = ” + processID);
}
//Retrieve active stage
function retrieveActiveStage(){
//Get active stage
var activeStage = Xrm.Page.data.process.getActiveStage();
var stageName = activeStage.getName();
var stageID = activeStage.getId();
alert(“Acitve stage is ” + stageName + ” ID = ” + stageID);
}
//event handler for Stage Change
function onStageChange(){
alert(“On stage change event”);
}
//event handler for Stage Selected
function onStageSelected(){
alert(“On stage Selected event”);
}
I hope you find it useful!
Author: Zhe Chen
Title: Lead Dynamics CRM Consultant @ Adisys
Email: zhechen@adisys.co
HI, thanks for all this information,
I was trying to use the Xrm.Page.data.process.moveNext() function to get to the next stage that is from a different entity.
When I use it I get the error: “Cross Entity” that sounds reasonable because is a different entity,
but how can I pass to the next stage if it is from a different entity?
is there a way??
thanks
Iair
Lair – The stage must be one for the current entity. I don’t think it’s supported by JavaScript directly. However you may try to use real time workflow or plugin to have the stage moved to different entity.
Zhe Chen
Reblogged this on Nishant Rana's Weblog.
Reblogged this on CRM 2015 – Sreeni Pavalla's Dynamic CRM Blog.
I can’t seem to make the SDK 2016 example work. So this is awesome!