The CreatorCon Call for Content is officially open! Get started here.

Check to see if a user is a memeber of a group

Walter Toney
Tera Expert

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

 

answer = ifScript();

function ifScript() {
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', '5de5599e873706d047f2a9350cbb3507'); // The Group where we are looking for the User
    gr.addQuery('user', current.variables.common_default.var_requested_for); // Variable Set where the requested for is Located
    gs.info(current.variables.common_default.var_requested_for.toString()); 
    gr.query();
    if (gr.next()) {
        gs.info("user is real");
        return 'yes';   // push the group approval
    } else {
        gs.info("User is not Real");
        return 'no'; // go through the normal approval process

    }
}
7 REPLIES 7

PA001
Mega Sage

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,

Tried this and it didnt work. 

Ravi Gaurav
Giga Sage
Giga Sage

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/

tried this and it did not work