HOW TO SWITCH BUSINESS PROCESS FLOW IN DYNAMICS 365 USING JAVASCRIPT?

Let us see how to switch Business Process Flow BPF in Dynamics 365 of an Entity using JavaScript.
We come across scenarios where we need to switch a BPF based on change of form or on change of a Field value. BPFs currently have a limitation on the number of stages (30 max) and the number of conditional levels (5 deep). 

If you hit one of these limitations, it could be useful to create multiple BPFs and use JavaScript to switch between processes depending on specific field values.

  • Create two separate BPFs for the required entity.Business Process Flow1
    Pick GUID of the Business Process Flow 32 characters between %7b and %7d  (%7b   7CBE0333-7D3F-49F1-AAAF-0B2C746C67FB  %7d)
    Business Process Flow2
    Business Process flow is stored as a field “Processid” on the entity.
    • Create a Web Resource of Type Script(JScript)
    • Get the Current Processid associated with the form using following Command.


    var activeProcess = formContext.data.process.getActiveProcess();

    var activeProcessID = activeProcess.getId();

  • Check the condition based on either Form Name or Field Value.In order to fetch the current Form Name use the following command:var formType = Xrm.Page.ui.getFormType();
    var formName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
  • If form name and processed conditions are met then change BPF:if (formName = “ABC” && activeProcessID. toUpperCase() != 7CBE0333-7D3F-49F1-AAAF-0B2C746C67FB)
    formContext.data.process.setActiveProcess(“DEEC5EF0-9F6D-4D6D-8878-65E7473A9921”, “success”);
  • Following is the complete code:var formContext;
    function formBasedBPF(executionContext)
    {
    debugger;
    var formType = Xrm.Page.ui.getFormType();
    formContext = executionContext.getFormContext();
    var formName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
    var activeProcess = Xrm.Page.data.process.getActiveProcess();
    var activeProcessID = activeProcess.getId();
                 if (formType != 1 && formName == “Donor Cycle” && activeProcessID.toUpperCase() != “DEEC5EF0-9F6D-4D6D-8878-65E7473A9921”)
    formContext.data.process.setActiveProcess(“DEEC5EF0-9F6D-4D6D-8878-65E7473A9921”, “success”);
                 else if (formName == “Annual Fund Donor Cycle” && activeProcessID.toUpperCase() != “7CBE0333-7D3F-49F1-AAAF-0B2C746C67FB”)
    formContext.data.process.setActiveProcess(“7CBE0333-7D3F-49F1-AAAF-0B2C746C67FB”, “success”);
    }
  • Save and publish the web resource.
  • Open Form Properties using Form EditorIf the functionality has to work on change of a field add this on change Event Handler of the field.If the functionality has to work on change of the form add this on Load Event Handler of the form.
  • Save and publish the form.Based on the requirement try to change the field or change the form and Business Process Flow will also change.

Leave a Reply