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

Hi Adi,

gs.getUser() will give you the user who is currently logged in so that is not the case which JJG is looking here.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

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);

There are some small errors in the script but otherwise works well, thank you!