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, the existing Vaccination BPF on an existing Contact record was used to show this navigation to the Next Stage on change of text in the 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 it's 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 has 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["[email protected]"] = "/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, the 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["[email protected]"] = "/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
- This can be worked on new and existing records but make sure BPF Stage in selected record in first stage.
- 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.
- Stage 2 can also be fetched through code, but just for demo purposes value was hardcoded.
- Prior experience in 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.
- To handle for other stages then extra conditions to be written like vaccination status condition.
- Refresh contact record post JavaScript gets fired in case if you don’t see new stage population automatically.
- Basic knowledge of BPF and finding stages of the selected Contact record is required to understand this logic.
Conclusion
In this way, one can easily write JavaScript code so as to move automatically BPF Stages based on the given business requirement.