- 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-04-2020 06:03 AM
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:
Please mark this answer correct if it resolved the query and mark it helpful too if it helped you at all.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2020 06:05 AM
(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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2020 07:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2020 02:03 AM
Try
gs.getUser().getUserByID(user.sys_id).isMemberOf("Workers")
instead of
(!gs.getUser(groupadd.sys_id).isMemberOf('Workers'))