I need a business rule to hide some records based on a user role

Jerome MAISETTI
Mega Guru

Hello All

I have the need to hide certain business rules outside of a specific user role. So I have a True/False field in the sys_script table called "u_ebonding_business_rule".
If this field is set to true, those records can only be seen by users with the role "ebonding_admin".

I have created a query business rule with the code below but it doesn't work..

 

(function executeRule(current, previous /*, gs*/ ) {

  // Define the name of the role that can view the business rule
  var allowedRole = "ebonding_admin";

  // Check if the business rule has the 'ebonding business rule' flag set to true
  var ebondingFlag = current.getValue("u_ebonding_business_rule");
  if (ebondingFlag == true) {

    // Get the current user's role(s)
    var currentUser = gs.getUser();
    var userRoles = currentUser.getRoles();

    // Check if the user has the allowed role
    if (userRoles.indexOf(allowedRole) == -1) {

      // If the user does not have the allowed role, restrict visibility
      current.setDisplay(false);
      gs.info("Business rule " + current.name + " hidden from non-" + allowedRole + " users.");
    }
  }

})(current, previous);


Can someone help me ? 🙂

Thanks
Jérôme

1 ACCEPTED SOLUTION

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Jerome MAISETTI ,

 

Please try with this version of your business rule:

 

(function executeRule(current, previous /*, gs*/ ) {

  // Define the name of the role that can view the business rule
  var allowedRole = "ebonding_admin";
  
    // Get the current user's role(s)
    var currentUser = gs.getUser();
    var userRoles = currentUser.getRoles();

    // Check if the user has the allowed role
    if (userRoles.indexOf(allowedRole) == -1) {

      // If the user does not have the allowed role, restrict visibility
      current.addEncodedQuery('u_ebonding_business_rule=false');
      gs.info("Some Business rules are hidden for non-" + allowedRole + " users.");
    }
 
})(current, previous);

 

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

View solution in original post

6 REPLIES 6

Thanks @Simon Christens 

@Jerome MAISETTI , I've updated the BR script, please use that

Simon Christens
Kilo Sage

Hi

Query business rules doesnt work like that.

query business rules applies a filter - so you do not have access to "current" there
What needs to be done is something like:

 

 

 

if(!gs.getUser().hasRole('ebonding_admin')){

current.addQuery('u_ebonding_business_rule', false);
}

 

 

You could even add the role check in the condition field of the business rule so it only runs if users doent have the ebonding_admin role and then apply the current.addQuery() part in the script

This way you enforce an extra filter condition for users that doesnt have the ebonding_admin role

This is from the docs on when BRs runs

 

https://docs.servicenow.com/bundle/rome-application-development/page/script/business-rules/reference...