Dynamics CRM 2015 – Interact with the Business Process Flow controls in the form

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

Advertisements

Dynamics CRM 2015 Business Rules – Are you ready?

Dynamics CRM 2015 – Scope of Business Rules

Business Rules was introduced in Dynamics CRM 2013 which was restricted to client side only. Now with  latest CRM 2015, server side business rule is available which will help business user easily configure validation logic and reduce the development effort.

There are three levels of scope:

Scope Where it runs run
Entity All forms and server
All forms All forms
Specific form Just that form

 

In the top right of the form, use the Scope field to set the scope for the rule. “Entity” scope level is new in CRM 2015. Setting the scope of the business rule at an entity level, forces the business rule executed on both server and all clients side.

 

For example, on update of an entity record we will set “Description” field mandatory based on some conditions. So in this case we can create Business rule as below:

Business Rules #1

Business Rules #1

  • Server Side Validation

 

To test this business rule validation on server side, we create a real time workflow to update this record which should trigger the validation logic:

crm 2015 bpa2

Trigger the workflow from a record and get below error message immediately:

crm 2015 bpa3

The record cannot be saved due to “Business Process Error”:

crm 2015 bpa4

Download log file and we can find the trace details:

 

<TraceText>

[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.SyncWorkflowExecutionPlugin]

[1769c1b5-1784-e411-80c5-00155d016814: ] Starting sync workflow ‘Test Scope of Business Rule’, Id: 0e69c1b5-1784-e411-80c5-00155d016814

Entering UpdateStep1_step: Sync workflow ‘Test Scope of Business Rule’ terminated with error ‘Attribute ‘new_description’ cannot be NULL’

</TraceText>

 

  • Client Side Validation To test this business rule in client side, we just simply update the record on the form and the error message show up as expected:

 

crm 2015 bpa5

Conclusion:

 

To implement the same validation logic in previous CRM version, we have to develop plug-in in server side. Now CRM 2015 offers us a much simple way to apply the same logic on both server and all clients without writing coding. You can also set the rule select a specific form.

I hope you find it useful!

Author: Zhe Chen
Title: Lead Dynamics CRM Consultant @ Adisys
Email: zhechen@adisys.co