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.
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 ContactCustomizations and open existing web resource MoveBPFStage as shown in the below figure.
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.
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
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
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
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
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
Step 10
After Step 9, click on Ok, and refresh and observe stage Still as Vaccination Status only as shown in the below figure
Note
- 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.
- 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.
- 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.
- 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.