Business rule

karunasrikuna
Tera Contributor

Hi,

 

I need help on one of my requirement

I have created a group called "test". If any user gets added into the group having 2 roles "itil" , "admin" , then he should be automatically added to the group which i have created called "test" .

 

@Ankur Bawiskar or @Sohail Khilji can you plz help me on this.

1 ACCEPTED SOLUTION

AshishKM
Kilo Patron
Kilo Patron

Hi @karunasrikuna , 

 

Please check and confirm, are you checking both role or either one is enough for adding user in Test group.

If you are trying to check both roles then AND condition on same addQuery will not work becuase these are two different record and condition applied on same column Role. ( You can try in list view using AND, will not get any record )

 

The below is working for Role = itil OR Role = Admin and user is adding in test group, you can change the sys_id of this group.

 

For AND condition, we need to check twice. 

(function executeRule(current, previous /*null when async*/) {
	// sys_id of itil and admin role, you dont need to change
	var strQuery="role=282bf1fac6112285017366cb5f867469^ORrole=2831a114c611228501d4ea6c309d626d";
	var grGroupRole = new GlideRecord("sys_group_has_role");
		grGroupRole.addQuery("group", current.group);
		// filter for role itil and admin, sys_id will be same on all instance
       	grGroupRole.addQuery(strQuery);
		grGroupRole.query();
		// check if group has record, the role has AND condition
		if(grGroupRole.next()){
			var grGroupMember = new GlideRecord("sys_user_grmember");
				grGroupMember.initialize();
				grGroupMember.group = 'ac416fd4c32f0210339b9dfc05013194'; // sys_id of test group;
				grGroupMember.user = current.user;
				grGroupMember.insert();
		}
})(current, previous);

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

7 REPLIES 7

AshishKM
Kilo Patron
Kilo Patron

Hi @karunasrikuna , 

 

Please check and confirm, are you checking both role or either one is enough for adding user in Test group.

If you are trying to check both roles then AND condition on same addQuery will not work becuase these are two different record and condition applied on same column Role. ( You can try in list view using AND, will not get any record )

 

The below is working for Role = itil OR Role = Admin and user is adding in test group, you can change the sys_id of this group.

 

For AND condition, we need to check twice. 

(function executeRule(current, previous /*null when async*/) {
	// sys_id of itil and admin role, you dont need to change
	var strQuery="role=282bf1fac6112285017366cb5f867469^ORrole=2831a114c611228501d4ea6c309d626d";
	var grGroupRole = new GlideRecord("sys_group_has_role");
		grGroupRole.addQuery("group", current.group);
		// filter for role itil and admin, sys_id will be same on all instance
       	grGroupRole.addQuery(strQuery);
		grGroupRole.query();
		// check if group has record, the role has AND condition
		if(grGroupRole.next()){
			var grGroupMember = new GlideRecord("sys_user_grmember");
				grGroupMember.initialize();
				grGroupMember.group = 'ac416fd4c32f0210339b9dfc05013194'; // sys_id of test group;
				grGroupMember.user = current.user;
				grGroupMember.insert();
		}
})(current, previous);

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

can you please confirm in which table this code will work in BR

BR on Group Member [ sys_user_grmember

 

There is no filter condition becuased nothing to check at group level, group has role which is in other table sys_group_has_role, so this BR will execute on every insert/or/update in sys_user_grmember table. ( unless you update the "When to run" Insert or Update or both )

 

AshishKM_0-1721322012901.png

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi @AshishKM 

one more question , if those roles have been removed for the users then will they be removed from the group ?