Create a new catalog item to manage Group Membership

Karthick PS
Tera Contributor

Hi,

 

My client wants to manage the group membership (add or remove a member from a group).

 

So, I am proposing a catalog item to do this job.

Do we have any OOB catalog item for that?

If we do not, then can I get the script to be used in the workflow of the catalog item to do this job.

Please help.

5 REPLIES 5

Mike_R
Kilo Patron
Kilo Patron

There isn't a catalog item/workflow OOTB.

You can probably easily create a flow instead of a workflow, but if you want a workflow script, the solution posted here should work: https://www.servicenow.com/community/it-service-management-forum/add-remove-users-to-the-group/m-p/5...

@Mike_R - Thank you. Let me check this.

rajesh9885
Mega Guru

Hello Karthick

You need to create a catalog this with field like group that you need to modify and the user that you need to remove or add to the group

 

you can use following script for removing / add (make changes accordingly)

 

Note :- this is not a tested script.. please test it before implementing

 

function removeGroupsOfInactiveUser() {
var groupGR = new GlideRecord(‘sys_user_grmember’);
groupGR.addQuery(‘user’, current.u_employee_name);
groupGR.query();
while (groupGR.next()) {
groupGR.deleteRecord();
}
}

function removeRolesOfInactiveUser() {
var roleGR = new GlideRecord(‘sys_user_has_role’);
roleGR.addQuery(‘user’, current.u_employee_name);
roleGR.query();
while (roleGR.next()) {
roleGR.deleteRecord();
}
}

 

 

like wise assuming it would be multiple that you need to use for loop

 

var user = current.variables.<field name>;
var group = current.variables.<group field name>; //get the groups

var users = user.toString().split(',');
for(var i=0; i<users.length; i++)
{
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('user', users[i]);
userGR.addQuery('group', group);
userGR.query();
if(!userGR.next())
{
userGR.initialize();
userGR.user = users[i];
userGR.group = group;
userGR.insert();
}
}

Thanks @rajesh9885 . Will check on this as well.