Add or remove multiple users from the group

Lakshmi56
Tera Contributor

Requirement is manager of a particular group should be able to add multiple users to the selected group or remove multiple users from the selected group using catalog item.

when manager submits the request, users in "Add members to group" should be added and

users in "Remove members from group" should be removed from the group.

what should be the script in workflow("run Script").

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

update as this and test

var usersToAdd = current.variables.add_users; // give correct variable name here
var usersToRemove = current.variables.remove_users;  // give correct variable name here

var group = current.variables.group;  // give correct variable name here

var rec = new GlideRecord('sys_user_grmember');
rec.addQuery("group", group);
rec.addQuery("user", "IN", usersToRemove.toString());
rec.query();
rec.deleteMultiple();

var arr = usersToAdd.toString().split(',');

for(var i=0;i<arr.length;i++){
	var addRec = new GlideRecord('sys_user_grmember');
	addRec.addQuery("user", arr[i]);
	addRec.addQuery("group", group);
	addRec.query();
	if(!addRec.hasNext()){
		addRec.initialize();
		addRec.user = arr[i];
		addRec.group = group;
		addRec.insert();
	}
}

Regards
Ankur

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

View solution in original post

25 REPLIES 25

Hi @Ankur Bawiskar ,

added gs.info(),

find_real_file.png

but the message is populating twice in system logs:

find_real_file.png

is there any issue in the code?

Hi,

that should not happen but you can ignore it.

But it should work fine in both native + portal

Regards
Ankur

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

Hi @Ankur Bawiskar ,

but in native view, list is not filtering and showing as none.

how can this be achieved in native view?

The similar solution has worked for me in past in both native + portal

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

We are trying to achieve same requirement in human resource scope so delete members and add members in a particular group is not possible as we are not in global scope.

So we trying to write script include in global scope and catalog client script please guide us

Below I am attaching my script include and client script

 

Client script:

function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } //Type appropriate comment here, and begin script below var ga = new GlideAjax('removeUsers'); ga.addParam('sysparm_name', 'removeUs'); ga.addParam('sysparm_groupname', g_form.getValue('group_name')); ga.addParam('sysparm_groupusers', g_form.getValue('remove_members')); ga.getXML(checkRes); function checkRes(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); alert(answer); } }

 

Script include:

var removeUsers = Class.create(); removeUsers.prototype = { removeUs: function() { var groupName = this.getParameter('sysparm_groupname'); var users = this.getParameter('sysparm_groupusers'); var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('sys_id', groupName); gr.addQuery('sys_id', 'IN', users); gr.query(); gr.deleteMultiple(); return 'success'; }, type: 'removeUsers' };