How to auto populate users to group dynamically ?

Community Alums
Not applicable

Hello,

How to add new users to in a particular group, when a table is updated with new user.

ex: table : custome table
     Group : Service Incident Group

I know for this i have to write business rule, here i'm looking for script.

Thanks and Regards,

1 ACCEPTED SOLUTION

Manoj Kumar16
Giga Guru

write an 'after' 'insert' and 'update' business rule in the custom table-

var gr=new GlideRecord('sys_user_grmember');

gr.initialize();

gr.group="Sys_ID of Service Incident Group";// best practice is to get the sysID using a property

gr.user=current.FIELD_NAME_WHICH_CONTAINS_THE_NEW_USER_IN_CUSTOM_TABLE;

gr.insert();

View solution in original post

6 REPLIES 6

Manoj Kumar16
Giga Guru

write an 'after' 'insert' and 'update' business rule in the custom table-

var gr=new GlideRecord('sys_user_grmember');

gr.initialize();

gr.group="Sys_ID of Service Incident Group";// best practice is to get the sysID using a property

gr.user=current.FIELD_NAME_WHICH_CONTAINS_THE_NEW_USER_IN_CUSTOM_TABLE;

gr.insert();

Thank you, your script helped me. 

I also used the below to remove the user from the group when the condition reverses.

 

(function executeRule(current, previous /*null when async*/) {

var user = current.sys_id; //Field name which contains the new user in custom table. I'm using sys_user table so it's just sys_id.

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user',user);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}

})(current, previous);

 

Great !

You can also start using GlideQuery instead of GlideRecord-

var query = new global.GlideQuery('sys_user_grmember')
.where('user', current.sys_id)
.deleteMultiple();

 

A little warning - that GlideRecord and GlideQuery will remove the User from ALL Groups.  You need to add a condition to include the Group in the search as well.