Business rule to check if user is in a group, if not then add them to the group

JJG
Kilo Guru

Hello,

I have a business rule for the sys_user table that is supposed to check all users in the table to see if they have the employee box set to true or false. If it is true, it should add them to a group. It does not seem to be working, is there an error in my code?

Business Rule:

(function executeRule(current, previous /*null when async*/ ) {
var groupadd = new GlideRecord('sys_user');

groupadd.addQuery('employee', 'true');

groupadd.query();

while (groupadd.next())

{
if (groupadd.user.isMemberOf('Workers')) {
return;
}

else {
var rec1 = new GlideRecord('sys_user_grmember');
rec1.query();
rec1.user = groupadd.sys_id;
rec1.group.setDisplayValue('Workers');
rec1.update();
}}
})(current, previous);

1 ACCEPTED SOLUTION

Dubz
Mega Sage

I can't see a field called 'employee' on my PDI so i'm assuming this is a custom field you added yourself? If so it will be called u_employee.

Also, isMemberOf is a glide user method, you can't just use it on a sysID, you'd have to use gs.getUserByID(groupadd.user).isMemberOf('Workers');

Finally, if you're inserting a record into a table, start with initialize() and finish with insert. 

I would also avoid nesting glide records, try out the below, it should work.

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

var employees = [];

var groupadd = new GlideRecord('sys_user');
groupadd.addQuery('u_employee', 'true');
groupadd.addActiveQuery();
groupadd.query();

while (groupadd.next()){
employees.push(groupadd.getUniqueValue());
}

for(var i=0; i< employees.length; i++){

var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user', employees[i]);
rec1.addQuery('group.name', 'Workers');
rec1.query();

if(gr.hasNext()){
continue;
} else {
rec1.initialize();
rec1.user = employees[i];
rec1.group.setDisplayValue('Workers');
rec1.insert();
}
}

})(current, previous);

View solution in original post

7 REPLIES 7

Mohit Kaushik
Mega Sage
Mega Sage

Hi There,

When is your BR executing. What are the conditions which is triggering the rule. Like before insert/update or some extra condition in the condition field.

I would suggest if you want to check this everyday or at some specific interval then write a scheduled job script and you can use your same BR script for that.

refer the below link for scheduled job script:

https://docs.servicenow.com/bundle/orlando-application-development/page/script/useful-scripts/concep...

 

Please mark this answer correct if it resolved the query and mark it helpful too if it helped you at all.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Adi26
Tera Expert

(function executeRule(current, previous /*null when async*/ ) {
var groupadd = new GlideRecord('sys_user');

groupadd.addQuery('employee', 'true');

groupadd.query();

while (groupadd.next())

{
if (!gs.getUser(groupadd.sys_id).isMemberOf('Workers')) {
var rec1 = new GlideRecord('sys_user_grmember');
rec1.initialize();
rec1.user = groupadd.sys_id;
rec1.setDisplayValue('group','Workers');
rec1.insert();
}}
})(current, previous);

This does not seems to work:

if (!gs.getUser(groupadd.sys_id).isMemberOf('Workers')) {

Why is there the getUser method in this script?

In a business rule or other server script, the gs.getUser() method returns a user object. The user object is an internal representation of the currently logged in user and provides information about the user and various utility functions.

Try 

gs.getUser().getUserByID(user.sys_id).isMemberOf("Workers")

 instead of 

(!gs.getUser(groupadd.sys_id).isMemberOf('Workers'))