- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:19 AM
Hi,
we have a custom form wherein we want to assign tickets based on a round robin format.
Example- If there are 3 users User1, user2 and User3 then 1stticket should be assigned to User1, 2nd to user2, 3rd to user3 and then 4th again to User1 and so on.
For this i have created a field on form 'u_last_assigned_user' and created an Assignment Group and added the above 3 users to that group and created below business rule.
Before Insert and Update.
(function executeRule(current, previous /*null when async*/) {
// Ensure the assignment group is set
if (!current.assignment_group) {
return;
}
// Get the members of the assignment group
var groupMembers = [];
var group = new GlideRecord('sys_user_grmember');
group.addQuery('group', current.assignment_group);
group.query();
while (group.next()) {
groupMembers.push(group.user.sys_id.toString());
}
if (groupMembers.length === 0) {
// No members in the group
return;
}
// Get the last assigned user
var lastAssignedUser = current.u_last_assigned_user;
// Find the next user in the round-robin sequence
var nextAssignedUser = null;
if (lastAssignedUser) {
var lastIndex = groupMembers.indexOf(lastAssignedUser.toString());
var nextIndex = (lastIndex + 1) % groupMembers.length;
nextAssignedUser = groupMembers[nextIndex];
} else {
// If no last assigned user, start with the first user in the group
nextAssignedUser = groupMembers[0];
}
// Assign the next user and update the last assigned user
if (nextAssignedUser) {
current.assigned_to = nextAssignedUser;
current.u_last_assigned_user = nextAssignedUser;
} else {
// Fallback: If no users in the group, clear the assignee
current.assigned_to = '';
}
})(current, previous);
By using this the tickets are getting assigned to only 1 user, it is not assigning to other 2 users as expected.
Please help if there is any changes required to this code.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:28 AM
@Gunashekar Instead of complicating this logic any further, you should write a simple logic to fetch last two records which were created recently. Extract the assigned_to user from these two records and push them inside an array.
Now intersect your Group user array and the assignment user array (created previously) and assigned the ticket to the user to whom none of the previous two tickets were assigned.
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:28 AM
@Gunashekar Instead of complicating this logic any further, you should write a simple logic to fetch last two records which were created recently. Extract the assigned_to user from these two records and push them inside an array.
Now intersect your Group user array and the assignment user array (created previously) and assigned the ticket to the user to whom none of the previous two tickets were assigned.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 10:04 AM
Hi Sandeep,
Thank you for your input. I have made changes to code as below and it works. I just need to set the recent records limit to whatever number of users are in group in my case 3 so set it to 3.
(function executeRule(current, previous /*null when async*/) {
// Ensure the assignment group is set
if (!current.assignment_group) {
gs.info("Round Robin Assignment: No assignment group set", "RoundRobinAssignment");
return;
}
// Get the members of the assignment group
var groupMembers = [];
var group = new GlideRecord('sys_user_grmember');
group.addQuery('group', current.assignment_group);
group.query();
while (group.next()) {
groupMembers.push(group.user.sys_id.toString());
}
if (groupMembers.length === 0) {
gs.info("Round Robin Assignment: No members in the assignment group", "RoundRobinAssignment");
return;
}
// Get the last two assigned users from the most recent records
var lastTwoAssignedUsers = [];
var recentRecords = new GlideRecord(current.getTableName());
recentRecords.addQuery('assignment_group', current.assignment_group);
recentRecords.orderByDesc('sys_created_on');
recentRecords.setLimit(3);
recentRecords.query();
while (recentRecords.next()) {
if (recentRecords.assigned_to) {
lastTwoAssignedUsers.push(recentRecords.assigned_to.sys_id.toString());
}
}
// Determine the next user for assignment
var nextAssignedUser = null;
for (var i = 0; i < groupMembers.length; i++) {
if (lastTwoAssignedUsers.indexOf(groupMembers[i]) === -1) {
nextAssignedUser = groupMembers[i];
break;
}
}
// If all users were in the last two assignments, assign to the first user in the group
if (!nextAssignedUser) {
nextAssignedUser = groupMembers[0];
}
gs.info("Round Robin Assignment: Next assigned user - " + nextAssignedUser, "RoundRobinAssignment");
// Assign the next user
current.assigned_to = nextAssignedUser;
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 10:12 AM
(function executeRule(current, previous /*null when async*/) {
// Ensure the assignment group is set
if (!current.assignment_group) {
gs.info("Round Robin Assignment: No assignment group set", "RoundRobinAssignment");
return;
}
// Get the members of the assignment group
var groupMembers = [];
var group = new GlideRecord('sys_user_grmember');
group.addQuery('group', current.assignment_group);
group.query();
var groupCount = group.getRowCount(); //get the group memeber count.
while (group.next()) {
groupMembers.push(group.user.sys_id.toString());
}
if (groupMembers.length === 0) {
gs.info("Round Robin Assignment: No members in the assignment group", "RoundRobinAssignment");
return;
}
// Get the last two assigned users from the most recent records
var lastTwoAssignedUsers = [];
var recentRecords = new GlideRecord(current.getTableName());
recentRecords.addQuery('assignment_group', current.assignment_group);
recentRecords.orderByDesc('sys_created_on');
recentRecords.setLimit(groupCount); //Limit the query the number of members in the group
recentRecords.query();
while (recentRecords.next()) {
if (recentRecords.assigned_to) {
lastTwoAssignedUsers.push(recentRecords.assigned_to.sys_id.toString());
}
}
// Determine the next user for assignment
var nextAssignedUser = null;
for (var i = 0; i < groupMembers.length; i++) {
if (lastTwoAssignedUsers.indexOf(groupMembers[i]) === -1) {
nextAssignedUser = groupMembers[i];
break;
}
}
// If all users were in the last two assignments, assign to the first user in the group
if (!nextAssignedUser) {
nextAssignedUser = groupMembers[0];
}
gs.info("Round Robin Assignment: Next assigned user - " + nextAssignedUser, "RoundRobinAssignment");
// Assign the next user
current.assigned_to = nextAssignedUser;
})(current, previous);
This should do the job for you.
Don't forget to mark the response helpful and correct if it manages to answer your question.