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.

Restricting Groups availability through Business Query and not ACL for Request

Anki3
Tera Contributor

Hi All,

Once Request submitted from HR catalog Item, Request No will be generated Followed by --> RITM 

So this Request/RITM should be Visible to only HR & HRIS group & its members not to any one else other than this 2(HR/HRIS) groups.

 

I need solution through Query Business Rule.

I have tried the Query Business Rule but it is not working as Expected, I am adding same script below if any body can help me modyfying it or giving me correct solution would be helpful.

 

Before Query:

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

   if (!checkGroup())

            current.addQuery("cat_item", "!=", '7c2a1a638700c210558c2f0d8bbb356d');

                function checkGroup() {

                    var grpMember = new GlideRecord('sys_user_grmember');

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

                    grpMember.addQuery('group', '2222a6eb8700c210558c2f0d8bbb35da');

                    grpMember.query();

                    return grpMember.hasNext();

                }

            })(current, previous);

 

Thanks In Advance!

Anku.

1 REPLY 1

Community Alums
Not applicable

Hi @Anki3 ,

Try with this script-

(function executeRule(current, previous /*null when async*/) {
// Check if the current user is a member of the HR or HRIS group
if (!checkGroup()) {
// If not a member, add query condition to restrict visibility
current.addQuery('requested_for', gs.getUserID()); // Only allow visibility to the current user
current.addQuery('u_requested_for_group', 'NOT IN', ['HR', 'HRIS']); // Exclude HR and HRIS groups
}

function checkGroup() {
var user = gs.getUser();
var hrGroup = user.isMemberOf('HR');
var hrisGroup = user.isMemberOf('HRIS');
return hrGroup || hrisGroup;
}
})(current, previous);