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

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

Hello Thank you so much its working fine!! and I need to restrict duplicate users add into the group if you have any resolution, please help me with that. Thank you

Hello,

Please use below code

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()) // put not condition so that it rescricts the creation of the duplicate record 

{

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

}

Please mark this helpful if you fond it use ful

Palak Gupta
Tera Guru
Tera Guru

Hi there!

You have to use "current.variables.variable_name" instead of "current.variable_name".

Regards,

Palak