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
}

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.
  1. First, check if a process is assigned to the record
    var process = formContext.data.process;
    if (process){
    ...
    }
  2. Then, check if there is active process and if it is rendered
    var activeProcess = process.getActiveProcess();
    if (activeProcess && activeProcess.isRendered()) {
    ...
    }
  3. Finally, check for the active process stage
    var stage = process.getActiveStage();
    var stageName = stage.getName();
    if (stageName == "<somevalue>") {
    //Do something
    }
  4. 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!

Comments

  1. Thank for the code, he helps me a lot, by the way there is a little mistake, the line
    var stageName = stageObj.getName();
    should be replace with
    var stageName = stage .getName();

    ReplyDelete
    Replies
    1. Thank you Philippe for your feedback. Glad it helps you.
      You are right about the code, I updated it accordingly.

      Delete

Post a Comment

Popular posts from this blog

DYNAMICS 365 HOW TO HIDE RECENT RECORDS FOR LOOKUP FIELD IN UCI

SEARCH BY GUID IN DYNAMICS 365

SAVE FORM IN DYNAMICS 365 JAVASCRIPT