Creating a custom incident auto assignment. Need some assistance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 12:38 AM
HI,
(function executeRule(current, previous /*null when async*/) {
gs.log("Condition executing");
gs.log('Log for assignment group: ' + current.assignment_group);
// This if part is for reopen incident which can be assigned to previous resolved by and cannot be considered in round robin. Same wise i want to do for routing tickets as well. If i route the ticket to team B and team B routed back since this action be taken by team A only, then it should assign to same person in Team A who routed and cannot be considered in round robin.
if (current.state == '1' && previous.state == '6' && previous.u_resolved_by) {
current.assigned_to = previous.u_resolved_by;
} else {
var availableUser = findNextAvailableUser();
gs.log("Available user: " + availableUser);
if (availableUser) {
current.assigned_to = availableUser.sys_id; // Assigning based on sys_id
} else {
gs.log("No available user found");
}
}
})(current, previous);
function findNextAvailableUser() {
var tableName = 'u_incident_auto_assignment_rule';
var users = new GlideRecord(tableName);
users.addQuery('u_active', 'true'); // Modified query condition
users.orderBy('sys_created_on');
users.query();
gs.log("Number of records found in custom table: " + users.getRowCount()); // Log number of records found
var assignedCount = {};
while (users.next()) {
assignedCount[users.u_user_name.user_id.toString()] = countAssignedIncidents(users.u_user_name.toString());
}
var minAssignments = Number.MAX_SAFE_INTEGER;
var nextUser;
for (var user in assignedCount) {
if (assignedCount[user] < minAssignments) {
minAssignments = assignedCount[user];
nextUser = user;
gs.log("Next user pick: " + nextUser);
}
}
if (nextUser) {
// Return the sys_id of the user
return {
sys_id: nextUser
};
}
return null;
}
function countAssignedIncidents(userId) {
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('assigned_to', userId);
incidentGr.addActiveQuery();
incidentGr.query();
return incidentGr.getRowCount();
}
Please assist. Do i am missing the proper DOTWALKING?
Thanks.