How to check if current user in particular group

Basina Akash
Tera Contributor

How to check if current user is in particular group if he is not in that group he will be added to that group 

10 REPLIES 10

how to create record in sys_user_grmember

 

 

Hi 

Use this script to create a membership 

var user = "SYS_id"; //User Sys_id
var member = new GlideRecord('sys_user_grmember');
member.initialize();
member.user = user;
member.group = 'SYS_ID'; //Group Sys_id
member.insert();

 

Mark my answer Helpful & Accepted if I have answered your question

Community Alums
Not applicable

Hi Akash,

You can use this script to create record.

 

var gr=new GlideRecord("sys_user_grmember");
gr.addQuery('group','sys_id');  //sys_id of the group.
gr.addQuery('user',gs.getUserID()); 
gr.query();
if(gr.next()){
return true;
//gs.log("true");
}
else{
gr.initialize();
gr.user=gs.getUserID();
gr.group='sys_id';  //sys_id of the group.
gr.insert();
}

 

Let me know if you are stuck.

 

Please mark it helpful if you feel this answered your query.

@Basina Akash 

 

to create record in Group Member table you can use below script

 

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.user = '<sys id of user>';
gr.group = '<sys id of group>';
gr.insert();

 

Thanks,

Danish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Basina Akash 

where are you checking it? the script will depend on that

what's your business requirement and what script did you start and where are you stuck?

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