Restrict Stage Movement Based On Roles Using JavaScript In BPF

Introduction

In certain implementations, BPF Stage movement is to be done based on roles. So, this can be achieved by writing custom JavaScript and logic to capture roles of the logged-in user. As an example, we consider here same vaccination BPF on Contact record, where for users with role salesman will be restricted for stage movement.

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.

Restrict Stage Movement Based on Roles using JavaScript in BPF

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.

Restrict Stage Movement Based on Roles using JavaScript in BPF

Step 3

After Step 2, open customizations solutions here in my example ContactCustomizations and open existing web resource MoveBPFStage as shown in the below figure.

Restrict Stage Movement Based on Roles using JavaScript in BPF

Step 4

After Step 3, in javascript write down the following statement to fetch roles of the logged-in user with below code

var roles = Xrm.Utility.getGlobalContext().userSettings.roles;

As shown in the below figure.

Restrict Stage Movement Based On Roles using JavaScript in BPF Figure 4

Step 5

After Step 4, take a variable and name it as restrictedRole and loop through roles that are retrieved from Step 4 and verify if any existing role with name salesman is present, if present then make restrictedRole Variable to true with the below code

var restrictedRole = false;​​​​​​​
roles.forEach(function(item) {
    if (item.name.toLowerCase() === "sales man") {
        restrictedRole = true;
    }
});

As shown in the below figure

Restrict Stage Movement Based On Roles using JavaScript in BPF Figure 5

Step 6

After Step 5, if restrictedRole variable changes to true then write a condition check to verify restrictedRole variable, if it becomes true then alert user with restricted access and exit further code, code snippet looks like below

if (restrictedRole) {
    alert("Restricted Access");
    return;
}

As shown in the below figure

Restrict Stage Movement Based On Roles using JavaScript in BPF Figure 6

Step 7

After Step 6, final code looks like this

var roles = Xrm.Utility.getGlobalContext().userSettings.roles;
var restrictedRole = false;
roles.forEach(function(item) {
    if (item.name.toLowerCase() === "sales man") {
        restrictedRole = true;
    }
});
if (restrictedRole) {
    alert("Restricted Access");
    return;
}

As shown in the below figure

Restrict Stage Movement Based On Roles using JavaScript in BPF Figure 7

Step 8

After Step 7, publish customizations and then open current logged-in user record and make sure that he has “sales man role”. As shown in the below figure

Restrict Stage Movement Based on Roles using JavaScript in BPF

Step 9

After Step 8, navigate to contact record and try to change Job Title and current stage as Vaccination Status you should see popup as the logged-in user have “sales man” role which will restrict the remaining part of logic to move to next stage as shown in the below figure

Restrict Stage Movement Based on Roles using JavaScript in BPF

Step 10

​​​​​​​After Step 9, click on Ok, and refresh and observe stage Still as Vaccination Status only as shown in the below figure

Restrict Stage Movement Based on Roles using JavaScript in BPF

Note

  1. In this article, I have concentrated on actual logic related to the article, because of which post refresh title gets changed because of autosave feature.
  2. Here I cautiously selected a user which have at least 1 user role, in production code proper null checks and validations can also be done.
  3. Role of the user, I have hardcoded here, better way of doing it by storing this value in an environment variable and write code to retrieve value which I have not covered here as I have concentrated more on the logic related to this article.
  4. All the rest of the code will not execute, if role matches with sales man, here I have used existing web resource on Vaccination BPF.

Conclusion

In this way, one can easily write JavaScript code to restrict stage movement based on role for a given business requirement with less code.