- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 05:10 AM
Hello,
I've a requirement to check whether the user is part of a group. If it is then I've log it to file or something to do.
Here is my script and I'm not getting desired output, please let me know what is the issue with the script, is it because of dot walk fields. Please help me here.
var gr = new GlideRecord('sys_user');
gr.initialize();
gr.query();
while(gr.next()){
//gs.info(gr.user_name);
var gm = new GlideRecord('sys_user_grmember');
gm.initialize();
gm.addQuery('sys_user_grmember.user.sys_id',gr.sys_id);
gm.Query;
while(gm.next()){
gs.info('In');
if(gm.group.sys_id == 'aead02002f26011048be265df699b6a3'){
gs.info(gr.user_name);
//Do something
}
}
}
Appreciate any suggestions!!
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 07:17 AM
Hi,
then do this
var gr = new GlideRecord('sys_user');
gr.query();
while(gr.next()){
var gm = new GlideRecord('sys_user_grmember');
gm.addQuery('group.name', 'Capacity');
gm.addQuery('user', gr.getUniqueValue());
gm.query();
if(gm.next()){
gs.info("User " + gr.getDisplayValue() + ' is member of ' + 'Capacity');
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 05:34 AM
Hello Kishore,
If you just want to check some users from the server-side you can perform it via writing a BR
var grp = gs.getProperty('name of your group property'); //to get the group id via property
var userID = gs.getProperty('name of your user property'); // to get the user id via property
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userID);
gr.addQuery('group', grp);
gr.query();
if(gr.next()){
gs.info("User is Member of Group!");
//Do something
}
else{
gs.info("User is Not a Member of Group!");
//Dont do something
}
If you are want to perform some action on the basis of the user's presence in the group, Use the below code in BR
gs.getUser().isMemberOf(gs.getProperty('name of your property'))
Cheers..!
Happy Learning
Tushar
Cheers..!

Happy Learning:)

Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 08:52 AM
Thanks for the response.
But I'm looking for the script with in the Flow as I'm adding a custom action with script step.
I'll check the suggestions by all of you and will try.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 05:43 AM
Hi Kishore,
If you want users who are member of group whose sys_id is 'aead02002f26011048be265df699b6a3' then you don't need to glide into user table, you can achieve the same by gliding into group membership table as shown below,
var grGroupMember = new GlideRecord('sys_user_grmember');
grGroupMember.addQuery('group', 'aead02002f26011048be265df699b6a3');
grGroupMember.query();
while(grGroupMember.next()){
gs.print("User"+grGroupMember.user.name+" is part of group"+grGroupMember.group.name);
}
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP
Above will Returns true if the user is a member of the group you specified in your property. Returns false if not a member of that group. So with the above, it will execute (true) for users part of that group.