Automate Adding user to Group and Remove user from Group through workflow on catalog item?

Krishna N
Tera Contributor

Hello,

I have to create a workflow for adding user to group or  remove user from group automatically after approving RITM and task will create after adding the user to group or remove from group, Based on selection of Add or Remove choice list in the catalog item.

Catalog variables are: 1.Requesting for, 2.choice list (Add/Remove)

please help me with procedure and provide any script for this automation for remove or add user to group based on choice selection

I tried to add user with below script in Run script activity but empty record is added to the group

var groupQuery = new GlideRecord('sys_user_grmember');
groupQuery.initialize();
groupQuery.user = current.variables.requesting_for; //whatever your current user field is
groupQuery.group = "group_sys_id"; // need to mention sys id of the group 
groupQuery.insert();

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello ,

Please try below script for adding into group

var groupQuery = new GlideRecord('sys_user_grmember');
groupQuery.initialize();
groupQuery.user = current.variables.requesting_for; //whatever your current user field is
groupQuery.group = "group_sys_id"; // need to mention sys id of the group 
groupQuery.insert();

For removing from the group

var groupDel= new GlideRecord('sys_user_grmember');
groupDel.addQuery('user',current.variables.requesting_for);
 
groupDel.groupDel.addQuery('groups','group sys_id');  // need to mention sys id of the group 

groupDel.query();

if(groupDel.next())

{
groupDel.deleteRecord();

}

Please accept this solution if it is helpful 

thanks

View solution in original post

5 REPLIES 5

Hello @Krishna N,

For avoiding duplicate entries you can update your code as below

var groupQuery = new GlideRecord('sys_user_grmember');

groupQuery.addQuery('user', current.variables.requesting_for);
groupQuery.addQuery('group', 'group_sys_id'); // need to mention sys id of the group 

groupQuery.query();
if (!groupQuery.next())

{

groupQuery.initialize();
groupQuery.user = current.variables.requesting_for; 
groupQuery.group = "group_sys_id"; // need to mention sys id of the group 
groupQuery.insert();

}

Regards,

Palak