Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict Module visibility to certain groups

Ken61
Giga Guru

Hello All,

I created a "Incident Dashboard" module under the Incident application. The requirement is to restrict visibility to only admin and groups where the “Include members” (u_include_members) field is true in the sys_user_group table.

I used below before query business rule, but its not working. I am new to scripting.

I will also like to use system property to store the module sys_id instead of directly calling the sys_id from the script

(function executeRule(current, previous /null when async/ ) {
// Add your code here
var opst = new GlideRecord("sys_user_grmember");
opst.addEncodedQuery("userDYNAMIC" + gs.getUserID() + "^group.u_include_members=true");
opst.query();
if (!opst.next()) {
current.addQuery('sys_id', '!=', '3D2ea6922d0451b650e898e6c94f784125'); //sys_id of the module

}



})(current, previous);

 

 

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Ken61 

so if logged in user is admin or logged in user is member of any group where "u_include_members" is true then that user should see

try this

-> ensure in query BR add this condition !gs.hasRole('admin')

-> create property of type string and store sysid there

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var opst = new GlideRecord("sys_user_grmember");
    opst.addEncodedQuery("user=" + gs.getUserID() + "^group.u_include_members=true");
    opst.query();
    if (!opst.hasNext()) {
        current.addQuery('sys_id', '!=', gs.getProperty('propertyName')); //sys_id of the module
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader