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

Thanks a Lot Ankur, It helped me. Marking your answer as correct 

Hi @Ankur Bawiskar ,

 

Need help in one scenario.

 

In my Account User getting created and adding into Group 'X'.

The requirement is Based on user group membership need to populated value in Business Unit and BU column.

 

Note: Both BU and Group details we are pulling it from sys_user_group table only.

e__rajesh_badam_0-1742995385366.png

 

@e__rajesh_badam 

can you post a new question for this and tag me there?

share complete business requirement and screenshots and also what script did you try so far and what debugging did you perform?

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

kunal20
Kilo Guru

Hi,

You can achieve this with the help of After Business rule on sys_user table.

once a record is inserted check the division and then Glide in the sys_user_group table and add the user there in its member.