How to check if user is member of one of the groups from a parent group

Khalnayak
Tera Guru

Hello,

I am creating a business rule in which I need to check if current logged in user is member of any of the child groups of a certain parent group.

And if user is a member of any of them groups, then code can continue and display the appropiate message.

How can I achieve this please. need code help

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Khalnayak ,

you can try this 

var arr=[];

var gr = new GlideRecord('sys_user_group');

gr.addQuery('parent','your_parrent_group_sys_id');

gr.query();

while(gr.next())

{

arr.push(gr.name.toString());

}

for(var i=0; i<arr.length; i++)

{

if(gs.getUser().isMemberOf(arr[i]))

{

//continue your code 

}

}

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can get first all the child groups of the parent and then query sys_user_grmember

var arr = [];

var gr = new GlideRecord("sys_user_group");
gr.addQuery("parent.name", "Group ABC");
gr.query();
while (gr.next()) {
	arr.push(gr.getValue('name'));
}


var gr1 = new GlideRecord("sys_user_grmember");
gr1.addQuery("group", "IN", arr.toString());
gr1.setLimit(1);
gr1.query();
if (gr1.hasNext()) {
 // member of any one of the child groups
}

regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader