How to reference current record fields in an encoded query for a Before/Query business rule

RobertBarnes
Mega Contributor

How to reference current record fields in an encoded query for a Before/Query business rule

       

We would be to use a Before/Query business rule that would filter the records based on the user's role. We can do this from an ACL but this is problematic, because most records will be blocked by the ACL and will result in only a couple of records on each of the list pages.   Each page will   include a "Number of rows removed from this list by Security constraints" message, where the user must page forward through many screens to find the few records that they need to access.

       

There does not seem to be a way to build the query based on a value in the "current" record object. The "current" record object can be accessed in the ACL script, but it
cannot be accessed from the script in the before Before/Query business rule.

       

For example:

Assume that a record has a field titled "Role"

How do you build a query string such as   gs.hasRole(current.role)?

       

This will work in the ACL, but there does not appear to be a way to get it into an encoded record query for the Before/Query business rule.

1 ACCEPTED SOLUTION

Hi @Robert Barnes



Thanks for clarifying your question.



Since you would like to make roles dynamic I would use a function to find all the roles and adding it into the query.



Following is something that can be done:


var encodedQuery = '';


var userRoles = _getLoggedInUserRoles();


encodedQuery = encodedQuery + '^ORrolesCONTAINS'+userRoles;


current.addEncodedQuery(encodedQuery);



function _getLoggedInUserRoles(){


      userRoles = [];


      var grHasRole = new GlideRecord('sys_user_has_role');


      grHasRole.addQuery('user',gs.getUserID());


      grHasRole.query();


      while(grHasRole.next()) {


              userRoles.push(grHasRole.role.name);


      }


      return userRoles;


}



I hope this helps.


View solution in original post

5 REPLIES 5

HI Brajendra,



I too have similar requirement. But the issue, I am facing is, it is overriding module level filters instead of adding. Please let me know if there is any way to achieve this.