top of page

HOW TO ADVANCE YOUR BUSINESS PROCESS FLOWS WITH CODE (PART I).

To advance your Business Process Flows in an automated manner, there are several ways to do it on the server side or the client side. In this blog, you will see examples of how to achieve this.



Before diving into how to accomplish it, let's explain what a Business Process Flow (BPF) is.

BPFs are components available in model-driven Power Apps that guide users through a series of steps and stages within a table's form.



When creating such processes, an additional table is generated in our Dataverse that we need to use to advance our BPF on the server side.


To advance these processes, the first thing we need to know is the GUID of the stages in our processes. To do this, we need to execute the following in a browser window:



https://org.crm4.dynamics.com/api/data/v9.0/processstages?$select=stagename&$filter=processid/workflowid eq <processID>

By replacing the highlighted fields with our own.


This will display the following:


{
"@odata.context":"https://org.crm4.dynamics.com/api/data/v9.0/$metadata#processstages(stagename)",
   "value":[
       {
           "@odata.etag":"W/\"761116\"",
           "stagename":"Stage 4",
           "processstageid":"73df2774-b083-4325-9804-845186962671"
       },
       {
           "@odata.etag":"W/\"761118\"",
           "stagename":"Stage 1",
           "processstageid":"7da32b9a-6994-ff0b-0cdc-b9c0c3bde713"
       },
       {
           "@odata.etag":"W/\"761112\"",
           "stagename":"Stage 2",
           "processstageid":"5148b38e-5fcd-4459-98e2-c778710192dd"
       },
       {
           "@odata.etag":"W/\"761114\"",
           "stagename":"Stage 3",
           "processstageid":"97b24b6c-0ac6-4d7d-8388-f47fec8af6fc"
       }
    ]
}

In this code, we will find the GUIDs of the stages that we will have in the BPF to prepare our automations and make the process advance.


Advancing the BPF on the client side


To advance our processes on the client side, we need to use JavaScript. We'll need to program the event within the form of our table using event handlers. In the example, we'll execute our JS code when a dropdown field called "mar_stage" (of type option) is changed.


First, we need to create a web resource within our solution of type "Script" with the following code:


function AdvanceBPF(executionContext) {
     var formContext = executionContext.getFormContext();
     var StageValue = formContext.getAttribute("mar_stage").getValue();
     var BPFInstanceId = formContext.data.process.getInstanceId();      
     var StageInicio = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
     var StageFin = "73df2774-b083-4325-9804-845186962671";
    switch(StageValue){
        //Stage 1
        case 809620000:
            StageInicio = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
            StageFin = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
        break;
        //stage 2
        case 809620001:
            StageInicio = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
            StageFin = "5148b38e-5fcd-4459-98e2-c778710192dd";
        break;
        //stage 3
        case 809620002:
            StageInicio = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
            StageFin = "97b24b6c-0ac6-4d7d-8388-f47fec8af6fc";
        break;
        //stage 4
        case 809620003:
            StageInicio = "7da32b9a-6994-ff0b-0cdc-b9c0c3bde713";
            StageFin = "73df2774-b083-4325-9804-845186962671";   
        break;
        default:
        break;
    }
     var entity = {};
     entity["activestageid@odata.bind"] = "/processstages(" + StageFin + ")"; 
     entity["traversedpath"] = StageInicio + "," + StageFin;

    Xrm.WebApi.updateRecord("new_fasesdemo", BPFInstanceId, entity).then(
        function success(result) {
            var updatedId = result.id;
            console.log(updatedId);
        },
        function(error) {
            console.log(error.message);
            alert("Ooohh hay un error");
        }
    );
}

Next, we will add the new library to the form of the table and configure the event when the "mar_stage" field is changed. It is important to remember to pass the execution context in this configuration.

To conclude, we need to test that our development works correctly. 😊


In the next blog, we will see how to advance the BPF on the server side using Power Automate.

bottom of page