To check user belongs to a group

heisen
Kilo Contributor

Script to check if the logged in user belongs to any one of the groups?( Not a specific group, any one of the group)

5 REPLIES 5

Omkar Mone
Mega Sage

Hi

You need to write a Script Include on sys_user_grmember from where you will return the sys_id's of the group of logged in user.

var usersysid = this.getParameter("sysparm_usersysID");// fetch sysid here


var gr = new GlideRecord("sys_user_grmember");


gr.addQuery("user",usersysid);


gr.query();


while(gr.next())


{


gs.print("User is in group::"+gr.group.getDisplayValue());//collect the groups here in an array and return them.


}

 

Jace Benson
Mega Sage
Just do a gliderecord against sys_user_grmember for the given user. If you get a result they are a member of a group.

ServiceNow SA
Kilo Guru

If you want this to check for a particular record in table, then create a BR on that table and use below script:

var group = current.assignment_group;
if(!gs.getUser().isMemberOf(group.toString())) {

//Add your action

 

}

Dubz
Mega Sage

You can query the sys_user_grmember table to get that data. Where are you triggering this? on the incident form?

checkGroups();
function checkGroups(){
var arr = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', gs.getUserID());
gr.query();
while(gr.next()){
arr.push(gr.group.getDisplayValue());
}
gs.log('user is a member of these groups: ' = arr);
}