We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to check if currently logged in user is in one of the groups - ACL script

Kasia5
Tera Contributor

Hi All,

I have a watch_list field in a form and I need to create an ACL because this field should be visible only when user is in one of the groups which name starts with ABC or DEF. How can I write a script in ACL to achieve this?

I was trying for example with:

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', gs.getUser());
gr.addQuery('group', 'groupSTARTSWITHabc^ORgroupSTARTSWITHdef');
gr.query();

if (gr.next()) {
    answer = true;
} else {
    answer = false;
}

Thanks in advance for help

2 REPLIES 2

Mohith Devatte
Tera Sage

Hye ,

Please try below script

if(gs.getUser().isMemberOf('ABC') ||gs.getUser().isMemberOf('DEF') )

{

answer=true;

}

else

{

answer=false;

}

Please accept this solution if you find it helpful 

Voona Rohila
Giga Patron

Hi Kaisa

small changes in your code, 

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', gs.getUser());
gr.addEncodedQuery('groupSTARTSWITHabc^ORgroupSTARTSWITHdef'); 
//gr.addQuery('group', 'STARTSWITH','abc^ORgroupSTARTSWITHdef');//you can also use this.
gr.query();
if (gr.next()) {
    answer = true;
} else {
    answer = false;
}

solution provided by mohit also works.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
5x ServiceNow MVP