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

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
}
}

tried this and it did not work