分享方式:


範例:Xrm.Page.data.process.getEnabledProcesses

 

發佈日期: 2017年1月

適用對象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

此範例示範如何使用 Xrm.Page.data.process.getEnabledProcesses 方法,取得關於為實體啟用的程序的資訊。

需求

範例 JavaScript 程式庫的 Sdk.formOnLoad 函數必須設定為表單的 OnLoad 事件處理常式。

示範

Xrm.Page.data.process.getEnabledProcesses 方法傳回為實體啟用之程序的資訊,該程序可供目前使用者切換選取。 若要使用 Xrm.Page.data.process.setActiveProcess 方法,您可以使用 getEnabledProcesses 方法取得關於您可以設定的所有有效程序的資訊。

此範例說明 the Xrm.Page.data.processsAPI 中一些方法的使用方式。 並不代表使用此 API 符合業務需求,這只是示範如何使用程式碼存取關鍵屬性值。

範例

Sdk.formOnLoad 使用 Xrm.Page.data.process.getEnabledProcesses 方法,非同步擷取關於為實體啟用的商務程序流程的資訊。 範例傳遞匿名函數做為第一個參數。 在資料傳回時,此函數會以非同步方式執行,而且將資料傳遞為匿名函數的參數。

有關啟用商務程序流程的資訊提供為字典物件,其中程序 Id 是屬性名稱,商務程序流程名稱是屬性的值。 範例程式碼處理此資訊與並將在全域 Sdk.enabledProcesses 陣列設定值,供稍後執行的邏輯擷取。 範例也循環執行 Sdk.enabledProcesses 陣列中的值,並使用 Sdk.writeToConsole 函數將關於已擷取的商務程序流程的資訊寫至主控台。

//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);

當您在開啟瀏覽器開發人員工具時執行此範例,以下為針對已啟用多個商務程序流程的實體,寫至主控台的輸出範例。

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

另請參閱

為商務程序流程撰寫指令碼
範例:Xrm.Page.data.process.getActivePath

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權