How to get all the groups a user is in ?

Hrishabh Kumar
Giga Guru

I have sys_id of a user and I need to get the list of groups in which the is user present.

I need to achieve this via Script.

1 ACCEPTED SOLUTION

Harshal Aditya
Mega Sage
Mega Sage

Hi @Hrishabh Kumar ,

 

Hope you are doing well.

 

Please try below code -

 

var getGroup = new GlideRecord("sys_user_grmember");
getGroup.addQuery("user","user sys_id");
getGroup.query();
gs.log(getGroup.getRowCount());
while(getGroup.next()){
gs.log(getGroup.group.name);
}

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

View solution in original post

6 REPLIES 6

Harshal Aditya
Mega Sage
Mega Sage

Hi @Hrishabh Kumar ,

 

Hope you are doing well.

 

Please try below code -

 

var getGroup = new GlideRecord("sys_user_grmember");
getGroup.addQuery("user","user sys_id");
getGroup.query();
gs.log(getGroup.getRowCount());
while(getGroup.next()){
gs.log(getGroup.group.name);
}

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

Thanks a lot! 

This was exactly what I was looking for.

 

Does it also gets the groups in which the user is manager?

Hi @Hrishabh Kumar ,

 

If you want to get groups in which the user is manager

It will be easily done by querying sys_user_group table

 

var getGroup = new GlideRecord("sys_user_group");
getGroup.addQuery("manager","user sys id");
getGroup.query();
gs.log(getGroup.getRowCount());
while(getGroup.next()){
gs.log(getGroup.name);
}

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

 

 

 

Thanks for the accurate solution.