Add users into group Automatically based on Business Unit

Insider
Giga Guru

1: When a new user is added to ServiceNow, check their division is "A" and add them to the group "G".
2: When a user is made inactive, check their division is "A" and remove them from the group "G".

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

should be simple enough.

Is division a field on User table which will be populated during insertion?

if yes then use this

1: When a new user is added to ServiceNow, check their division is "A" and add them to the group "G".

-> After insert BR on sys_user table

Condition: Division == 'A'

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var rec = new GlideRecord('sys_user_grmember');
	rec.initialize();
	rec.user = current.sys_id;
	rec.group = 'groupA SysId';
	rec.insert();

})(current, previous);

2: When a user is made inactive, check their division is "A" and remove them from the group "G".

-> After update BR on sys_user table

Condition: Active = False && Division  == 'A'

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var rec = new GlideRecord('sys_user_grmember');
	rec.addQuery("group", 'groupA SysId');
	rec.addQuery("user", current.sys_id);
	rec.query();
	if(rec.next()){
		rec.deleteRecord();
	}

})(current, previous);

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

should be simple enough.

Is division a field on User table which will be populated during insertion?

if yes then use this

1: When a new user is added to ServiceNow, check their division is "A" and add them to the group "G".

-> After insert BR on sys_user table

Condition: Division == 'A'

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var rec = new GlideRecord('sys_user_grmember');
	rec.initialize();
	rec.user = current.sys_id;
	rec.group = 'groupA SysId';
	rec.insert();

})(current, previous);

2: When a user is made inactive, check their division is "A" and remove them from the group "G".

-> After update BR on sys_user table

Condition: Active = False && Division  == 'A'

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var rec = new GlideRecord('sys_user_grmember');
	rec.addQuery("group", 'groupA SysId');
	rec.addQuery("user", current.sys_id);
	rec.query();
	if(rec.next()){
		rec.deleteRecord();
	}

})(current, previous);

Regards
Ankur

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

Hi Ankur,

Will try with the following and let you know.

Thanks in advance,

@Insider 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

@Insider 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

 

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