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 

It's working now.

Q: I want to restrict the list of field Group Members which of type List Collector and is referencing to sys_user table to show only group members of the selected group.

can this be done?

Hi,

Glad to know that my script worked.

Please mark my response as correct and helpful to close the thread.

Regards
Ankur

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

Hi,

yes can be done via advanced ref qualifier on the User variable

you will have to query sys_user_grmember table and bring those users by passing the group sysId

Regards
Ankur

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

Hi @Ankur Bawiskar,

Thanks for the update on the question.

I have tried to "restrict the list of field Group Members which of type List Collector and is referencing to sys_user table to show only group members of the selected group"

it is working in portal view, but in native view script is not working.

Portal view:

Group Members field shows only the members of the group "AMERICAS Change Management"

find_real_file.png

Native view:

selected the same group "AMERICAS Change Management" but Group Members field is not populating the required list.find_real_file.png

Reference Qualifier of the variable:

find_real_file.png

Script Include:

find_real_file.png

Hi,

should work in native as well.

Did you try adding gs.info() in script include and check if function is called or not

regards
Ankur

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