Check to see if a user is a memeber of a group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:31 AM - edited 08-22-2024 06:31 AM
Team,
good day -
I have a request to add a process in workflow to push an approval if the "requested for" is a member of a group. other wise the process will move normallay. I have in the workflow a if statement but the script is not working and i was wondering what im doing wrong. can you advise
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:36 AM - edited 08-22-2024 06:38 AM
Try the below code and check
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', "Your Variable");
grMem.addQuery('group.name', 'Group Name');
grMem.query();
if(grMem.next()){
answer = 'yes';
}
else{
answer = 'no';
}
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 09:37 AM
Tried this and it didnt work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:37 AM
Hello,
Do Check the below revised code:-
answer = ifScript();
function ifScript() {
var gr = new GlideRecord('sys_user_grmember');
// Check if 'var_requested_for' is not null or undefined
var requestedFor = current.variables.common_default.var_requested_for;
if (!requestedFor) {
gs.info("Requested For is not defined.");
return 'no'; // Skip approval and move through the normal process
}
gr.addQuery('group', '5de5599e873706d047f2a9350cbb3507'); // The Group where we are looking for the User
gr.addQuery('user', requestedFor); // Variable Set where the requested for is Located
gr.query();
if (gr.next()) {
gs.info("User is a member of the group.");
return 'yes'; // Push the group approval
} else {
gs.info("User is not a member of the group.");
return 'no'; // Go through the normal approval process
}
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 09:37 AM
tried this and it did not work