- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2020 05:52 AM
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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2020 03:18 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2020 02:36 AM
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
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2020 03:18 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2020 05:16 AM
There are some small errors in the script but otherwise works well, thank you!