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

Prince Arora
Tera Sage
Tera Sage

@Jerome MAISETTI 

 

You can achieve the same thing using ACL by mentioning your condition as well roles.

Please check this Link 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

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!

thaaaaaaanks a lot! This did the trick 🙂

I would suggest you to look a bit more into the script accepted.

This is very bad practice
Query business rules doesnt have access to anything else than queries
So current.getValue() will always return undefined

Just a heads up