Dynamically Add and Remove Users to/from Group

Bruce11
Giga Expert

Hello,

We are looking to set up a automated process to add users to a specific group and remove them dynamically once or twice a day. Group membership criteria would be:

Active = true
Locked Out = False
Employee Type = Internal or External
Email Ends with "sec.com"

If people qualify they are put in the list, if they do not qualify they are removed. What would be the best way to achieve this with a script? Would appreciate if I can get a sample script for this.

Thank you.

1 ACCEPTED SOLUTION

Hi Bruce,

something like this

var rec = new GlideRecord('sys_user');
rec.addActiveQuery();
rec.addQuery('locked_out', false);
rec.addQuery('employee_type','IN','external,internal');
rec.query();
while(rec.next()){
	if(rec.email.toString().endsWith('sec.com')){
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', rec.sys_id);
		gr.addQuery('group.name', 'Group ABC');
		gr.query();
		if(!gr.next()){
			gr.initialize();
			gr.user = rec.sys_id;
			gr.group.setDisplayValue('Group ABC');
			gr.insert();
		}
	}
}

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Will there be any catalog item to automate this wherein users will raise request for adding/removing users to/from group

If yes then you can have catalog item designed for this and use workflow run script to add/remove users

Regards
Ankur

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

This is not for a catalog it. Just a group created to add to dashboard so users in it can view the dashboard. Any suggestion on how to achieve the use case?

Thanks

Hi,

then how it would be happening as dynamic unless you setup some process around this scenario

Regards
Ankur

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

What do you think the best approach would be in this case? Just looking for a way to auto assign active users that meet the criteria into the group. When active changes to false, then if they can be removed from group.