|
| 1 | +var assignedToId = ''; |
| 2 | +var minOpenTasks = 77777; |
| 3 | +var targetGroup = current.assignment_group; |
| 4 | + |
| 5 | +if (!targetGroup) { |
| 6 | + gs.addErrorMessage('Please select an Assignment Group first.'); |
| 7 | + action.setRedirectURL(current); |
| 8 | +} |
| 9 | + |
| 10 | +//Finding all active members in the target group |
| 11 | +var member = new GlideRecord('sys_user_grmember'); |
| 12 | +member.addQuery('group', targetGroup); |
| 13 | +member.query(); |
| 14 | + |
| 15 | +while (member.next()) { |
| 16 | + var userId = member.user.toString(); |
| 17 | + |
| 18 | + //CountIng the number of active tasks currently assigned to the member |
| 19 | + var taskCountGR = new GlideAggregate('task'); |
| 20 | + taskCountGR.addQuery('assigned_to', userId); |
| 21 | + taskCountGR.addQuery('active', true); |
| 22 | + taskCountGR.addAggregate('COUNT'); |
| 23 | + taskCountGR.query(); |
| 24 | + |
| 25 | + var openTasks = 0; |
| 26 | + if (taskCountGR.next()) { |
| 27 | + openTasks = taskCountGR.getAggregate('COUNT'); |
| 28 | + } |
| 29 | + |
| 30 | + //Checking if this member has fewer tasks than the current minimum |
| 31 | + if (openTasks < minOpenTasks) { |
| 32 | + minOpenTasks = openTasks; |
| 33 | + assignedToId = userId; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +//Assigning the current record to the chosen user |
| 38 | +if (assignedToId) { |
| 39 | + current.assigned_to = assignedToId; |
| 40 | + current.work_notes = 'Assigned via Smart Assign to the user with the fewest active tasks (' + minOpenTasks + ' open tasks).'; |
| 41 | + current.update(); |
| 42 | + gs.addInfoMessage('Incident assigned to ' + current.assigned_to.getDisplayValue() + '.'); |
| 43 | +} else { |
| 44 | + gs.addErrorMessage('Could not find an active member in the group to assign the task.'); |
| 45 | +} |
| 46 | + |
| 47 | +action.setRedirectURL(current); |
0 commit comments