Using JavaScript to Go to Next Stage in Business Process Flows

3 Comments
BEFORE WE START, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

Let’s take a look at how to move a Business Process Flow to the next stage using JavaScript.

If you prefer the video version, check out and subscribe to my YouTube channel:

Go to https://flow.microsoft.com and select to create a new Business Process Flow:

Let’s call it Account BPF:

We see:

Let’s make 3 stages – Stage 1, Stage 2, Stage 3:

. We will add some fields for each stage:

  • Stage 1 – Account Name
  • Stage 2 – Account Number
  • Stage 3 – Account Rating

Activate the BPF. Now we have our BPF on the account.

Let’s save this account record. With each Business Process Flow applied to a record, we have its own BPF record created. In our case, we called the BPF “Account BPF”. Using Advanced Find, we can see the Account BPF record and see an instance of it for Account “Test”, which is the one I just created:

Let’s check out the OData response for our BPF entity, which is called new_accountbpfs. I’m highlighting the Id of the Account, which is the instance of this flow. The account field here is _bpf_accountid_value:

URL – https://myorg.crm.dynamics.com/api/data/v9.1/new_accountbpfs

And the process id instance value for this instance is businessprocessflowinstanceid = 7998c058-c359-ea11-a811-000d3a35b7f2.

Now, we’ll need the Id of the BPF from the designer to move it with code. Grab the Id from the BPF URL:

Now running the request below where we’re using the Id from above:

https://yourorg.crm.dynamics.com/api/data/v9.0/processstages?$select=stagename&$filter=processid/workflowid eq 3a9f5db1-4d58-ea11-a811-000d3a35b480

We get a response back showing each stage and the GUID associated with the stage:

{"@odata.context":"https://yourorg.crm.dynamics.com/api/data/v9.0/$metadata#processstages(stagename)","value":[{"@odata.etag":"W/\"5592432\"","stagename":"Stage 1","processstageid":"2ada60b3-ded2-f549-4e05-4ce11f5ca891"},{"@odata.etag":"W/\"5592430\"","stagename":"Stage 3","processstageid":"3c54b635-63ad-4d56-a2d3-ae08f582249c"},{"@odata.etag":"W/\"5592428\"","stagename":"Stage 2","processstageid":"9e1f0d59-547e-4f47-b351-cf0deebb519f"}]}

I.e. Stage 1 has a process stage id of 2ada60b3-ded2-f549-4e05-4ce11f5ca891, Stage 2 has a process stage Id of 9e1f0d59-547e-4f47-b351-cf0deebb519f, stage 3 3c54b635-63ad-4d56-a2d3-ae08f582249c.

We will use these Ids shortly. Let’s add a field, and on change we will call JS to update our stage.

Now the code.

function AdvanceBPF(executionContext) {
     var formContext = executionContext.getFormContext();

     var Stage1 = "2ada60b3-ded2-f549-4e05-4ce11f5ca891";
     var Stage2 = "9e1f0d59-547e-4f47-b351-cf0deebb519f";

     var entity = {};
     entity["activestageid@odata.bind"] = "/processstages(" + Stage2 + ")"; 
     entity["traversedpath"] = Stage1 + "," + Stage2;

   var BPFId = "7998c058-c359-ea11-a811-000d3a35b7f2";
 
   var req = new XMLHttpRequest();
   req.open("PATCH", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/new_accountbpfs(" + BPFId + 
")", true);
   req.setRequestHeader("OData-MaxVersion", "4.0");
   req.setRequestHeader("OData-Version", "4.0");
   req.setRequestHeader("Accept", "application/json");
   req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
   req.onreadystatechange = function() {
       if (this.readyState === 4) {
           req.onreadystatechange = null;
 
           if (this.status === 204) {
               alert("Success");
           } else {
               alert("Error");
           }
       }
   };
   req.send(JSON.stringify(entity));
}

On running this by tabbing off our field, we see the BPF has is now at the next stage (note you may need to refresh the page to see this, or it may take a few seconds):

Next check out how to use MoveNext and MovePrevious to advance your BPFs in real time.

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

3 Responses to Using JavaScript to Go to Next Stage in Business Process Flows

  1. I did this code and it is showing success msg but it didnt go to next stage automatically. I was waiting for a few min and also tried to refresh the page. but cannot go next stage. pls advise.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *