DYNAMICS 365 HOW TO CHECK IF BUSINESS PROCESS FLOW (BPF) EXISTS ON A RECORD
Working with Business Process Flows is very popular and requested for many scenarios.
But it is sometimes tricky especially when you have some treatment to do in JavaScript.
Some of these scenarios might be changing the BPF phase if a field is set to a specific value or
show/hide fields/tabs based on the BPF phase... that require some JavaScript code in order to achieve them.
For some developers, it is straight forward to capture the process stage by entering the following lines of code.
var stage = formContext.data.process.getActiveStage();
var stageName = stage.getName();
if (stageName == "<somevalue>") {
//Do something
}
var stage = formContext.data.process.getActiveStage();
var stageName = stage.getName();
if (stageName == "<somevalue>") {
//Do something
}
This is enough and will perfectly work for new records, since the BPF will be automatically assigned to the record. However, for old records
that exist before the BPF is created, the above code won't work and it will give errors because no BPF is assigned to those records.
Continue reading if you want to know how to check if a Business Process Flow (BPF) is assigned to a record and to avoid errors in JavaScript.
Continue reading if you want to know how to check if a Business Process Flow (BPF) is assigned to a record and to avoid errors in JavaScript.
-
First, check if a process is assigned to the record
var process = formContext.data.process;
if (process){
...
}
-
Then, check if there is active process and if it is rendered
var activeProcess = process.getActiveProcess();
if (activeProcess && activeProcess.isRendered()) {
...
}
-
Finally, check for the active process stage
var stage = process.getActiveStage();
var stageName = stage.getName();
if (stageName == "<somevalue>") {
//Do something
}
-
At the end, the complete code will be as below
var process = formContext.data.process;
if (process){
var activeProcess = process.getActiveProcess();
if (activeProcess && activeProcess.isRendered()) {
var stage = process.getActiveStage();
var stageName = stage.getName();
if (stageName == "<somevalue>") {
//Do something
}
}
}
Hope This Helps!
Thank for the code, he helps me a lot, by the way there is a little mistake, the line
ReplyDeletevar stageName = stageObj.getName();
should be replace with
var stageName = stage .getName();
Thank you Philippe for your feedback. Glad it helps you.
DeleteYou are right about the code, I updated it accordingly.