Jaa


Sample: Xrm.Page.data.process.getEnabledProcesses

 

Applies To: Dynamics CRM 2015

This sample demonstrates how to retrieve information about processes enabled for an entity by using the **Xrm.Page.data.process.**6b79eb8c-8293-4d3a-a3a7-ea35157cde37#BKMK_getEnabledProcesses method.

Requirements

The Sdk.formOnLoad function in the sample JavaScript library must be set as the OnLoad event handler for a form.

Demonstrates

The **Xrm.Page.data.process.**6b79eb8c-8293-4d3a-a3a7-ea35157cde37#BKMK_getEnabledProcesses method returns information about the processes enabled for an entity that are available for the current user to switch to. If you want to use the **Xrm.Page.data.process.**6b79eb8c-8293-4d3a-a3a7-ea35157cde37#BKMK_setActiveProcess method, you can use the getEnabledProcesses method to get information about any valid processes you could set.

This sample illustrates the use of some of the methods in the Xrm.Page.data.processs API. It doesn’t represent using this API to meet a business requirement; it’s only intended to demonstrate how the key property values can be accessed in code.

Example

The Sdk.formOnLoad function uses the Xrm.Page.data.process.getEnabledProcesses method to asynchronously retrieve information about business process flows that are enabled for the entity. The sample passes an anonymous function as the first parameter. This function is executed asynchronously when the data is returned and the data is passed as the parameter to the anonymous function.

The information about enabled business process flow is provided as a dictionary object where the Id of the process is the name of the property and the name of the business process flow is the value of the property. The sample code processes this information and sets the values in a global Sdk.enabledProcesses array to be accessed by logic that executes later. The sample also loops through the values in uses the Sdk.enabledProcesses array and uses the Sdk.writeToConsole function to write information about the retrieved business process flows to the console.

//A namespace defined for SDK sample code
//You should define a unique namespace for your libraries
var Sdk = window.Sdk || { __namespace: true };
(function () {
 //A global variable to store information about enabled business processes after they are retrieved asynchronously
 this.enabledProcesses = [];

 // A function to log messages while debugging only
 this.writeToConsole = function (message) {
  if (typeof console != 'undefined')
  { console.log(message); }
 };

 //Code to run in the OnLoad event 
 this.formOnLoad = function () {
  //Retrieve Enabled processes
  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)
    }
   }

   //Any code that depends on the Sdk.enabledProcesses array needs to be initiated here

  });
 };

}).call(Sdk);

When you run this sample with the browser developer tools open, the following is an example of the output written to the console for an entity with multiple business process flows enabled.

Enabled business processes flows retrieved and added to Sdk.enabledProcesses array.
These are the enabled business process flows for this entity:
id: 7994be68-899e-4a40-8d18-f5c3b6940188 name: Sample Lead Process
id: 919e14d1-6489-4852-abd0-a63a6ecaac5d name: Lead to Opportunity Sales Process

See Also

Write scripts for business process flows

© 2016 Microsoft. All rights reserved. Copyright