- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 05:53 AM
Hello All,
I'm attempting to build a round robin business rule script that will need to look at a couple of a custom fields:
- u_roundrobin on the sys_user_group table as true/false.
- "u_out_of_office" (true/false) on the sys_user table
What I want to do with my business rule, is:
1) Query the current.assignment_group to see if u_roundrobin = true
2) If true, query all members of that group and only include those who's u_out_off_office = false
My code so far:
(function executeRule(current, previous /*null when async*/) {
var gMember = new GlideRecord('sys_user_grmember');
gMember.addQuery('group', current.assignment_group);
gs.print("assignmentGroup:"+gMember);
gs.log("assignmentGroup:"+gMember);
var rrMember = gMember.addQuery('u_roundrobin', true);
gs.print("rrMember:"+rrMember);
gs.log("rrMember:"+rrMember);
if(rrMember == true)
{
var agroup = new GlideRecord('sys_user_grmember');
agroup.addQuery('group', current.assignment_group);
var ooo = agroup.addQuery('u_outlook_out_of_office', true);
agroup.query();
gs.print("ooo:"+ooo);
gs.log("ooo:"+ooo);
if (ooo == true){
if (gMember.next()) {
current.assigned_to = gMember.user;
}
}
}
})(current, previous);
I'm struggling with the logic of query group to determine if u_roundrobin is true, then multiple if's or if I can do it all within a query and only reference members of the current assignment group (if it's a member of roundrobin and the user is not outofoffice (ooo).
thoughts?
thank you.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 06:20 AM
@JGerrity Try if the following works.
(function executeRule(current, previous /*null when async*/) {
var assignedGroup = current.getValue('assignment_group');
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id', assignedGroup);
if (group.next()) {
var isMemberRR = group.u_member_of_round_robin;
gs.print("isMemberRR:" + isMemberRR);
gs.log("isMemberRR:" + isMemberRR);
if (isMemberRR == true) {
var mem = new GlideRecord('sys_user_grmember');
mem.addQuery('group', group.getValue('sys_id'));
mem.orderBy('user.u_last_assigned_task_date');
mem.query();
if (mem.next() && mem.user.u_outlook_out_of_office != '1') {
current.assigned_to = mem.user;
}
}
}
})(current,previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 07:12 AM
when is the business rule running? on which table and what's the condition?
Your business rule should be Before insert/update.
(function executeRule(current, previous /*null when async*/) {
// Query the assignment group to check if u_roundrobin is true
var group = new GlideRecord('sys_user_group');
if (group.get(current.assignment_group) && group.u_roundrobin) {
// Query all members of the group who are not out of office
var gMember = new GlideRecord('sys_user_grmember');
gMember.addQuery('group', current.assignment_group);
gMember.addQuery('user.u_out_of_office', false);
gMember.query();
// If there are members available, assign the incident to the first one found
if (gMember.next()) {
current.assigned_to = gMember.user;
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 05:44 AM
yes you can use daily scheduled job and handle it
something like this but please enhance
(function() {
// Step 1: Collect all users in round-robin groups
var users = [];
var group = new GlideRecord('sys_user_group');
group.addQuery('u_roundrobin', true);
group.query();
while (group.next()) {
var gMember = new GlideRecord('sys_user_grmember');
gMember.addQuery('group', group.sys_id);
gMember.query();
while (gMember.next()) {
users.push(gMember.user.email.toString()); // Collect user email addresses
}
}
// Step 2: Make API call to check AutoReply status
var requestBody = {
"requests": users.map(function(email) {
return {
"url": "/users/" + email + "/mailboxSettings",
"method": "GET"
};
})
};
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://graph.microsoft.com/v1.0/$batch');
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN'); // Replace with your access token
request.setRequestBody(JSON.stringify(requestBody));
var response = request.execute();
var responseBody = JSON.parse(response.getBody());
// Step 3: Update u_out_of_office status based on API response
responseBody.responses.forEach(function(res) {
var email = res.id.split('/')[1];
var autoReplyStatus = res.body.automaticRepliesStatus;
var user = new GlideRecord('sys_user');
user.addQuery('email', email);
user.query();
if (user.next()) {
user.u_out_of_office = (autoReplyStatus === 'enabled');
user.update();
}
});
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 06:20 AM
@JGerrity Try if the following works.
(function executeRule(current, previous /*null when async*/) {
var assignedGroup = current.getValue('assignment_group');
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id', assignedGroup);
if (group.next()) {
var isMemberRR = group.u_member_of_round_robin;
gs.print("isMemberRR:" + isMemberRR);
gs.log("isMemberRR:" + isMemberRR);
if (isMemberRR == true) {
var mem = new GlideRecord('sys_user_grmember');
mem.addQuery('group', group.getValue('sys_id'));
mem.orderBy('user.u_last_assigned_task_date');
mem.query();
if (mem.next() && mem.user.u_outlook_out_of_office != '1') {
current.assigned_to = mem.user;
}
}
}
})(current,previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 07:12 AM
when is the business rule running? on which table and what's the condition?
Your business rule should be Before insert/update.
(function executeRule(current, previous /*null when async*/) {
// Query the assignment group to check if u_roundrobin is true
var group = new GlideRecord('sys_user_group');
if (group.get(current.assignment_group) && group.u_roundrobin) {
// Query all members of the group who are not out of office
var gMember = new GlideRecord('sys_user_grmember');
gMember.addQuery('group', current.assignment_group);
gMember.addQuery('user.u_out_of_office', false);
gMember.query();
// If there are members available, assign the incident to the first one found
if (gMember.next()) {
current.assigned_to = gMember.user;
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 05:40 AM
Hi @Ankur Bawiskar & @Sandeep Rajput ,
I'm looking to extend the functionality of this script and wondered if you had recommendations on logical approach (I'll try to figure out the coding part)...gotta learn right?
What I'm looking to do is iterate through the members of all assignment groups that have the group.u_roundrobin field set to true. Add all of these users into an array, then convert all users to input for an API action that will use MS Graph to check the autoreply status of Exchange Online via a batch API call. Then for all users who's autoreply status is enabled, set the u_outofoffice value to true on the user table.
Basically, it's looking to set the out of office flag to true based upon Exchange Online AutoReply status daily so our users only have to maintain out of office in one location (Outlook).
What I'm wondering is does this logic make sense? iterate through the assignment groups, add users to array and then add each user to an input variable of a flow action (script step), which will then be used to make the graph API call.
I was thinking a scheduled job (script) to only make the API call once or twice a day. The script needs to find all group members of round robin, set those users' email addresses (UPN) as input variables to then be called by an Flow (Action) which makes the API batch call, to check only those users.
Cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 05:44 AM
yes you can use daily scheduled job and handle it
something like this but please enhance
(function() {
// Step 1: Collect all users in round-robin groups
var users = [];
var group = new GlideRecord('sys_user_group');
group.addQuery('u_roundrobin', true);
group.query();
while (group.next()) {
var gMember = new GlideRecord('sys_user_grmember');
gMember.addQuery('group', group.sys_id);
gMember.query();
while (gMember.next()) {
users.push(gMember.user.email.toString()); // Collect user email addresses
}
}
// Step 2: Make API call to check AutoReply status
var requestBody = {
"requests": users.map(function(email) {
return {
"url": "/users/" + email + "/mailboxSettings",
"method": "GET"
};
})
};
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://graph.microsoft.com/v1.0/$batch');
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN'); // Replace with your access token
request.setRequestBody(JSON.stringify(requestBody));
var response = request.execute();
var responseBody = JSON.parse(response.getBody());
// Step 3: Update u_out_of_office status based on API response
responseBody.responses.forEach(function(res) {
var email = res.id.split('/')[1];
var autoReplyStatus = res.body.automaticRepliesStatus;
var user = new GlideRecord('sys_user');
user.addQuery('email', email);
user.query();
if (user.next()) {
user.u_out_of_office = (autoReplyStatus === 'enabled');
user.update();
}
});
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader