We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Dynamic Groups

sharukh
Tera Contributor

Is it possible dynamically to add or remove members based on the Role from the Groups.

For Example: I have created a group called Test and added a role "it_demand_manager" to the group. when a new user is created and added a role "it_demand_manager" to that user. Then that user is automatically should be added to the "test" Group.is it possible for that type of configuration or if u have any reference documents.please share this now.

Thanks in Advance

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron

@sharukh Create following business rule on sys_user_has_role table.

 

Replace role with your custom role it_demand_manager in filter conditions

Screenshot 2023-06-21 at 8.19.32 PM.png

Replace sys_id of the role mentioned on line number 5 with your own role.

Screenshot 2023-06-21 at 8.20.23 PM.png

 

Here is the script for the business rule.

 

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

	// Add your code here
	var groupRole = new GlideRecord('sys_group_has_role');
	groupRole.addQuery('role','2831a114c611228501d4ea6c309d626d');//Replace with sys_id of your role
	groupRole.query();
	if(groupRole.next()){
		var groupMember = new GlideRecord('sys_user_grmember');
		groupMember.initialize();
		groupMember.setValue('group',groupRole.getValue('group'));
		groupMember.setValue('user',current.user);
		groupMember.insert();
	}

})(current, previous);

Hope this helps,

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron

@sharukh Create following business rule on sys_user_has_role table.

 

Replace role with your custom role it_demand_manager in filter conditions

Screenshot 2023-06-21 at 8.19.32 PM.png

Replace sys_id of the role mentioned on line number 5 with your own role.

Screenshot 2023-06-21 at 8.20.23 PM.png

 

Here is the script for the business rule.

 

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

	// Add your code here
	var groupRole = new GlideRecord('sys_group_has_role');
	groupRole.addQuery('role','2831a114c611228501d4ea6c309d626d');//Replace with sys_id of your role
	groupRole.query();
	if(groupRole.next()){
		var groupMember = new GlideRecord('sys_user_grmember');
		groupMember.initialize();
		groupMember.setValue('group',groupRole.getValue('group'));
		groupMember.setValue('user',current.user);
		groupMember.insert();
	}

})(current, previous);

Hope this helps,

Thank you so much @Sandeep Rajput .It was helpful