Back to Basics # 32: Move to Next Stage using JavaScript in BPF

Introduction:

At times we must work on BPF Stages and navigate Stages automatically based on the business requirement, in this post we are going to see how it can be achieved through javascript .As an example, existing Vaccination BPF on an existing Contact record was used to show this navigation to Next Stage on change of text in title field.

Step 1:

Login to the required environment and go to flows and select Business process flows – Vaccination and observe whether BPF is active or not, if not then activate it as shown in the   below figure.

Step 2:

After Step 1, as this BPF was written on the top of contact record, open any existing contact record as shown in the   below figure.

Step 3:

After Step 2, open customizations solutions here in my example ContactCustomizations and create a webresoruce and name it as MoveBPFStage and save and publish customization   as shown in the   below figure.

Step 4:

After Step 3, we must write JavaScript logic in MoveBPFStage web resource and inside function MoveBPFStage to read the current stage get details of BPF Stage id and stage name and active stage along with currentBPFInstanceID as with the below code

var formContext = executionContext.getFormContext();

var activeStage = formContext.data.process.getActiveStage();

var stageId = activeStage.getId();

var stageName = activeStage.getName();

var currentBPFInstanceID= formContext.data.process.getInstanceId();

As shown in the   below figure.

Step 5:

After Step 4, now on contact entity write code to extract data from text field title OOB field using formcontext and write logic only if title field have value with below code

var title = formContext.getAttribute(“jobtitle”).getValue();

As shown in the below figure

Step 6:

After Step 5, if the stage name is not null and stage name is “Vaccination Status” then frame entity to pass to patch function to update BPF Stage using CRM API call by passing next stage id and activestageid and traverse path which is mandatory for a BPF Entity with the below code

// We have to pass guid of Created BPF 2nd Stage which will be found under //https://org30d0cff5.crm.dynamics.com/api/data/v9.1/new_vaccinations – //new_vaccinations is my bpf entity.

                var Stage2 = “ec4e9b57-fc52-4bad-909c-976f3f6bf975”;

                    var entity = {};

                    entity[“activestageid@odata.bind”] = “/processstages(” + Stage2 + “)”;

                    entity[“traversedpath”] = stageId + “,” + Stage2;

As shown in the below figure

Step 7:

After Step 6, above entity object to be passed to API Call using the below code

var req = new XMLHttpRequest();

                        req.open(“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v9.0/new_vaccinations(“+currentBPFInstanceID+”)”, 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)

                                    {

                                    //Success – No Return Data – Do Something

                                    alert(“Success”);

                                    }

                                    else

                                    {

                                    alert(“Error”);

                                    Xrm.Utility.alertDialog(this.statusText);

                                    }

                            }

                        };

                        req.send(JSON.stringify(entity));

As shown in the below figure

Step 8:

After Step 7, complete code looks like this below

function MoveBPFStage(executionContext) {

var formContext = executionContext.getFormContext();

var activeStage = formContext.data.process.getActiveStage();

var stageId = activeStage.getId();

var stageName = activeStage.getName();

var currentBPFInstanceID= formContext.data.process.getInstanceId();

var title = formContext.getAttribute(“jobtitle”).getValue();

if (title!=’undefined’ && title!=”)

    {

        //Stage2 – ec4e9b57-fc52-4bad-909c-976f3f6bf975

        if(stageName!=null && stageName ==”Vaccination Status”)

            {  

                // We have to pass guid of Created BPF 2nd Stage which will be found under https://org30d0cff5.crm.dynamics.com/api/data/v9.1/new_vaccinations – new_vaccinations is my bpf entity.

                var Stage2 = “ec4e9b57-fc52-4bad-909c-976f3f6bf975”;

                    var entity = {};

                    entity[“activestageid@odata.bind”] = “/processstages(” + Stage2 + “)”;

                    entity[“traversedpath”] = stageId + “,” + Stage2;

                    var req = new XMLHttpRequest();

                        req.open(“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v9.0/new_vaccinations(“+currentBPFInstanceID+”)”, 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)

                                    {

                                    //Success – No Return Data – Do Something

                                    alert(“Success”);

                                    }

                                    else

                                    {

                                    alert(“Error”);

                                    Xrm.Utility.alertDialog(this.statusText);

                                    }

                            }

                        };

                        req.send(JSON.stringify(entity));

                    }

            }

    }

As shown in the below figure

Step 9:

After Step 8, navigate to contact record and observe the current BPF Stage as shown in the below figure

Step 10:

After Step 9, now change title and observe javascript get’s fired and success message showed as an alert as shown in the below figure

Step 11:

After Step 10, now click on ok on contact record and observe changed new stage Provide Discount as shown in the below figure

Note:

  1. This can be worked on new and existing records but make sure BPF Stage in selected record in first stage.
  2. If there are any mandatory fields to be filled in BPF Stages then those values have to be passed through javascript code which is not explained here.
  3. Stage 2 can also be fetched through code, but just for demo purposes value was hardcoded.
  4. Prior experience to javascript is needed to understand this code and also importance is given to explain the logic so as to not take out the essence of the article.
  5. To handle for other stages then extra conditions to be written like vaccination status condition.
  6. Refresh contact record post java script gets fired in case if you don’t see new stage population automatically.
  7. Basic knowledge of BPF and finding stages of the selected Contact record required to understand this logic.

Conclusion: In this way, one can easily write Java Script code so as to move automatically BPF Stages based on the given business requirement.

2 thoughts on “Back to Basics # 32: Move to Next Stage using JavaScript in BPF

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind December 2021 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s