How to Add or Remove Users in Group.(Using Workflow).

Para5
Tera Guru

1. I have created one catalog item "Add Remove client portal Admnistrators".
2.On this catalog item added two variables:
    1.Contact to Add - When the User move in Selected, Automatically add to the 'Client portal group'
    2.Contact to Remove - When User move in Available,Automatically remove from the 'Client portal group'.
3.My requirement is adding or removing users from the 'Client portal group'. I want to implement this using workflow.
   Thank You.

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

try this

var usersToAdd = current.variables.contact_to_add.toString().split(','); // give correct variable name here

var usersToRemove = current.variables.contact_to_remove.toString(); // give correct variable name here

var rec = new GlideRecord('sys_user_grmember');
rec.addQuery("group.name", "Client portal group");
rec.addQuery("user", 'IN', usersToRemove);
rec.query();
while(rec.next())
    rec.deleteRecord();

for(var i in usersToAdd){
    var rec1 = new GlideRecord('sys_user_grmember');
    rec1.addQuery("user", usersToAdd[i]);
    rec1.addQuery("group.name", "Client portal group");
    rec1.query();
    if(!rec1.hasNext()){
        rec1.initialize();
        rec1.user = usersToAdd[i];
        rec1.setDisplayValue('group', "Client portal group");
        rec1.insert();
    }
}

regards
Ankur

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

View solution in original post

14 REPLIES 14

Musab Rasheed
Tera Sage
Tera Sage

Hello,

These threads have code, please tweak accordingly

https://community.servicenow.com/community?id=community_question&sys_id=9dcb3e211bde0d109337ece6b04bcb17

https://community.servicenow.com/community?id=community_question&sys_id=aba38f65dbd8dbc01dcaf3231f9619e4

https://community.servicenow.com/community?id=community_question&sys_id=e69161d61bcc309017d162c4bd4bcbf1

Please hit like and mark my response as correct if that helps

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

should be an easy script

Sharing sample workflow run script; please enhance it

var usersToAdd = current.variables.variableA.toString().split(','); // give correct variable name here

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

var rec = new GlideRecord('sys_user_grmember');
rec.addQuery("group.name", 'Client portal group');
rec.addQuery("user", 'IN', usersToRemove);
rec.query();
while(rec.next())
	rec.deleteRecord();

for(var i in usersToAdd){
	var rec1 = new GlideRecord('sys_user_grmember');
	rec1.addQuery("user", usersToAdd[i]);
	rec1.addQuery("group.name", "Client portal group");
	rec1.query();
	if(!rec1.hasNext()){
		rec1.initialize();
		rec1.user = usersToAdd[i];
		rec1.setDisplayValue('group', "Client portal group");
		rec1.insert();
	}
}

Regards
Ankur

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

Hello @Ankur Bawiskar 

Your scrips works perfectly, Thank You.

1.I can add Group Member sucessfully in the Group "Client portal group".

find_real_file.png

2. But when I removed the Group Members from the 2nd Variable i.e. variableB ,there is  still empty record is present in Group Members related list.

find_real_file.png

Hi,

the logic is correct.

can you share the script here?

which user is that one which shows empty?

Regards
Ankur

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