API for woring with Business Process Flows in Dynamics CRM

Hello everybody,

so I continue to familiarize myself with Dynamics CRM and for now my eyes were caught by Business Process Flows. 

And of course, what can be interesting for programmer in any environment? Of course programming features. 

And for client side Microsoft introduced Xrm.Page.process api.

Below will go list of methods, which are available there.

1. var activeProcess = Xrm.Page.data.process.getActiveProcess();

 With this method you can check is there any active process which is somehow binded to current record. It gives you process object, from which you can discover id of the process, name of process, etc. Very important part it has method getStages which will give you a list or a collection of stages of this process. 

2. var activePathCollection = Xrm.Page.data.process.getActivePath();

It is much more cool function. It gives you a collection of stages, but this collection has the following information: previous stages that have been completed, the current stage that's currently active and predicted future stages.

NB important note about future stages: it will be built on basics of current infromation in entity. So if user will modify some data which affect future stages, then future stages will be subject of modification. 

3. Xrm.Page.data.process.getEnabledProcesses

This function is interesting from viewpoint of usage. Microsoft provides following template of usage of it:

Xrm.Page.data.process.getEnabledProcesses(function (processes) {
   //Move processes to the global Sdk.enabledProcesses array;
   for (var processId in processes) {
    Sdk.enabledProcesses.push({ id: processId, name: processes[processId] })
   }
   Sdk.writeToConsole("Enabled business processes flows retrieved and added to Sdk.enabledProcesses array.");

   //Write the values of the Sdk.enabledProcesses array to the console
   if (Sdk.enabledProcesses.length < 0) {
    Sdk.writeToConsole("There are no enabled business process flows for this entity.");
   }
   else {
    Sdk.writeToConsole("These are the enabled business process flows for this entity:");
    for (var i = 0; i < Sdk.enabledProcesses.length; i++) {
     var enabledProcess = Sdk.enabledProcesses[i];
     Sdk.writeToConsole("id: " + enabledProcess.id + " name: " + enabledProcess.name)
    }
   }

this function gives you list of available processes for the user. And in your code, you can use 

4. Xrm.Page.data.process.setActiveProcess(processId, callbackFunction);

in order to switch user to that process.

5. setDisplayState allows you to expand or collapse the controls but with preserving visibility. 

 6. setVisible show or hide some stage. Can be useful for cases if you want to hide some stages as some that is possible to skip.

7. setActiveStage allows to jump to some stages. For example you can complete some stage sooner then needed or vice versa, move to first step in stage.

8. movePrevious. It is analogues of setActiveStage, with difference, that you don't care about providing id of previous stage.

But that's not all what is needed for effective involvement into activities flows. Another part which is important is Events or if to say more widely

Process Flow Events

1. OnStageChange - occurs when a stage changes

2. OnStageSelected - occurs when a new stage is selected for preview.

Few interesting details about those events:

a. you can't block user from changing stage except with help of required fields or plugins that will throw errors.

b. In order to use those function, you need to register those methods via addOnStageChange and addOnStageSelected methods. 

You would do it through the onLoad script. 

No Comments

Add a Comment