diff --git a/Client-Side Components/UI Actions/Smart Assign to available member/README.md b/Client-Side Components/UI Actions/Smart Assign to available member/README.md new file mode 100644 index 0000000000..664161b011 --- /dev/null +++ b/Client-Side Components/UI Actions/Smart Assign to available member/README.md @@ -0,0 +1,18 @@ +**Use-case:** +The primary goal of this UI Action is load-balancing. +It assigns tasks based on the fewest currently Active tasks assigned to a member in a group. + +**Example Scenario**: +An assignment group has 10 members. Instead of assigning a new task to the whole group or any random member, the user/agent clicks on +"Smart Assign" to find the member with the fewest currently Active tasks in the same group and assign the task to them. + +**UI Action Name:** +Smart Assign + +**Condition**: !current.assignment_group.nil() && current.assigned_to.nil() + +**How it works:** +1. The code queries the Group members table to find every single user associated with the currently selected assignment group. + If someone removes a previous assignement group and then clicks on Smart Assign button, they are shown an error message to choose an Assignment group. +2. There is a loop on the task table. This loop uses GlideAggregate to count how many active records are assigned to a specific user. +3. It tracks the user that has the lowest count of tasks assigned to them and assigns the current task to them. diff --git a/Client-Side Components/UI Actions/Smart Assign to available member/smartAssigntoAvailablemember.js b/Client-Side Components/UI Actions/Smart Assign to available member/smartAssigntoAvailablemember.js new file mode 100644 index 0000000000..0317deb5c9 --- /dev/null +++ b/Client-Side Components/UI Actions/Smart Assign to available member/smartAssigntoAvailablemember.js @@ -0,0 +1,47 @@ +var assignedToId = ''; +var minOpenTasks = 77777; +var targetGroup = current.assignment_group; + +if (!targetGroup) { + gs.addErrorMessage('Please select an Assignment Group first.'); + action.setRedirectURL(current); +} + +//Finding all active members in the target group +var member = new GlideRecord('sys_user_grmember'); +member.addQuery('group', targetGroup); +member.query(); + +while (member.next()) { + var userId = member.user.toString(); + + //CountIng the number of active tasks currently assigned to the member + var taskCountGR = new GlideAggregate('task'); + taskCountGR.addQuery('assigned_to', userId); + taskCountGR.addQuery('active', true); + taskCountGR.addAggregate('COUNT'); + taskCountGR.query(); + + var openTasks = 0; + if (taskCountGR.next()) { + openTasks = taskCountGR.getAggregate('COUNT'); + } + + //Checking if this member has fewer tasks than the current minimum + if (openTasks < minOpenTasks) { + minOpenTasks = openTasks; + assignedToId = userId; + } +} + +//Assigning the current record to the chosen user +if (assignedToId) { + current.assigned_to = assignedToId; + current.work_notes = 'Assigned via Smart Assign to the user with the fewest active tasks (' + minOpenTasks + ' open tasks).'; + current.update(); + gs.addInfoMessage('Incident assigned to ' + current.assigned_to.getDisplayValue() + '.'); +} else { + gs.addErrorMessage('Could not find an active member in the group to assign the task.'); +} + +action.setRedirectURL(current);