How to check if currently logged in user is in one of the groups - ACL script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2022 05:11 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2022 05:16 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2022 05:26 AM
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
2022-25 ServiceNow Community MVP