Back to Basics # 33: Restrict Stage Movement Based on Roles using JavaScript in BPF

Introduction:

In certain implementations, BPF Stage movement 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 sales man 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 sales man 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:

  1. In this article, I have concentrated on actual logic related to the article, because of which post refresh title get’s changed because of auto save 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 hard coded 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 Java Script code to restrict stage movement based on role for a given business requirement with less code.

2 thoughts on “Back to Basics # 33: Restrict Stage Movement Based on Roles using JavaScript in BPF

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind December 2021 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s