Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

check if the user is in among multiple groups

RudhraKAM
Tera Guru

Hello I have a Requirement were on the catalog item we have a field call "product_user " up on request in the work flow we need to write a if condition to check if the user is among any mentioned 6 groups , if yes then it should go for an approval for a particular user if not no ,,

 

can some one help me with the code 

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

you can try something like this.

 

i am adding one script for one group and you can add other groups to check by adding "OR" || operator.

 

var ourUser = gs.getUser().­getUserByID(current.variables.product_user);
answer = ifScript();

function ifScript()
{
if(­ourUser.­isMemberOf(put group sysid/name in single quote) || ourUser.­isMemberOf(put second group sysid/name in single quote)  || ourUser.­isMemberOf(put third group sysid/name in single quote))
{
return 'yes';
}
return 'no';
}

 

note: you can add as much condition in if block by using || operator to check the group

View solution in original post

6 REPLIES 6

Try below script:


var gr2 = new GlideRecord('sys_user_group');

gr2.addEncodedQuery(" add encoded query of all the 6 groups");
gr2.query();
while(gr2.next())
{
var groupsys = gr2.sys_id;
gs.addInfoMessage(sys +groupsys);
}
var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('group', groupsys);
gr.addQuery('user',current.product_user );
gr.query();
if (gr.next()) {

gs.log('query ran');


gs.log('This person is ' + gr.user + ' is a member');

Harsh Vardhan
Giga Patron

you can try something like this.

 

i am adding one script for one group and you can add other groups to check by adding "OR" || operator.

 

var ourUser = gs.getUser().­getUserByID(current.variables.product_user);
answer = ifScript();

function ifScript()
{
if(­ourUser.­isMemberOf(put group sysid/name in single quote) || ourUser.­isMemberOf(put second group sysid/name in single quote)  || ourUser.­isMemberOf(put third group sysid/name in single quote))
{
return 'yes';
}
return 'no';
}

 

note: you can add as much condition in if block by using || operator to check the group