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.

How is use isMemberOf for multiple group

Sadashiva Das
Tera Expert

How is use isMemberOf for multiple   group

7 REPLIES 7

mike_donathan
Kilo Guru

The function .isMemberOf only accepts one argument at a time (either the group name or sys_id). If you want to pass multiple groups to it, you can loop through an array like this:



var checkuser = 'don.goodliffe';


var checkGroupsArray = ['Database San Diego', 'Hardware', 'ITSM Engineering', '8a5055c9c61122780043563ef53438e3'];



for(var x=0; x < checkGroupsArray.length; x++){


  var isMember = gs.getUser().getUserByID(checkuser).isMemberOf(checkGroupsArray[x]);


  gs.print(isMember);


}




This will loop through each of the 3 groups listed in the array and print out a "true" or "false" if the user is a member of each group checked.



Alternatively, you can use .getMyGroups to return an array of a user's group memberships, the run those against whatever data set you'd like.




var checkuser = 'don.goodliffe';


var usersGroups = gs.getUser().getUserByID(checkuser).getMyGroups();


gs.print(usersGroups);



You could also build a GlideRecord query to run against sys_user_grmember, pass the user to the query to return the user's group memberships. Those values can then be checked against a list of values, or you can script it in such a way that if the user is a member of specific groups, the take an action.



var checkuser = 'don.goodliffe';




var groupMemberships = new GlideRecord('sys_user_grmember');


groupMemberships.addQuery('user.user_name', checkuser);


groupMemberships.query();




while(groupMemberships.next()){


  gs.print('User: ' + groupMemberships.user.name + ' is a member of: ' + groupMemberships.group.name);


}


Sadashiva Das
Tera Expert

Thanks for mail..



I am going to create an ACL with this   member of Either of two Assignment group '



but it is not working   kindly help in this .


if (answer == (gs.getUser().isMemberOf('Enterprise Applications and Architecture Team')) || (gs.getUser().isMemberOf('GIS_Operations_Effort')))


{


  answer=true;


}


Sadashiva Das
Tera Expert

my question is if user of eithor or on both Groups but not required for both Groups together . Kindly check the code and let me advice



if (answer == (gs.getUser().isMemberOf('Enterprise Applications and Architecture Team')) || (gs.getUser().isMemberOf('GIS_Operations_Effort')))


{


  answer=true;


}


if ((gs.getUser().isMemberOf('Enterprise Applications and Architecture Team')) || (gs.getUser().isMemberOf('GIS_Operations_Effort')))


{


  answer=true;


}