We will learn to dynamically switch the business process flow using JavaScript. Before we start, make sure to subscribe to CRM Crate so that you stay up-to-date in field of Dynamics 365.
Why do we need to dynamically switch the BPFs?
Consider a business scenario where we need to show a particular business process flow to the users with a particular job title. For example, the users with “Sales” job title will only be able to view and access the sales related BPF and the users with “Service” job title will only be above to view and access the service related BPF in the same record.
Using the setActiveProcess client API reference
We will use the setActiveProcess client API reference to automatically switch the business process flows as per the user inputs.
formContext.data.process.setActiveProcess(processId, callbackFunction);
If there is an active instance of the process, the table record is loaded with the process instance ID. If there is no active instance of the process, a new process instance is created and the table record is loaded with the process instance ID. If there are multiple instances of the current process, the record is loaded with the first instance of the active process as per the defaulting logic, that is the most recently used process instance per user.
JavaScript snippet code for dynamically updating the BPFs
The below JavaScript snippet is been triggered during the “OnChange” event of field “Switch BPF”.
function CallMethod (executionContext) {
var formContext = executionContext.getFormContext();
//Get Field Value.
var field = formContext.getAttribute("cc_switchbpf").getValue();
if(field == 1) // Optionset Value From 1st BPF
{
formContext.data.process.setActiveProcess("1D11AE47-1458-417E-B5C0-A1D87AFD868E");
}
else // Value Equivalent To 2nd BPF
{
formContext.data.process.setActiveProcess("A4CDF48D-3B45-4B41-8214-6F268AA5E7D9");
}
}
Validate the output in Dynamics 365
Once the above JavaScript snippet has been deployed, validate the output in the Dynamics 365 form as shown below.
Thus we learned to automatically switch between multiple business process flows using the JavaScript.