Creating a custom incident auto assignment. Need some assistance

AnandKumar1
Tera Expert

HI,

 

I have created a custom table u_incident_auto_assignment_rule
In which I have 2 field values.
u_user_name - with type sys_user reference.
u_active= true/false type.
 
AnandKumar1_0-1709800577346.pngAnandKumar1_1-1709800700729.png

 

 
My plan of execution is based on the true if 4 people are available. Based on the true value incident should assign the round robin basis.
 
I have created a custom business rule in addition for the table "incident". But I am not able to print this  
var assignedCount = {};
 
I am thinking i am missing the proper dotwalking here.
 
(function executeRule(current, previous /*null when async*/) {
    gs.log("Condition executing");
    gs.log('Log for assignment group: ' + current.assignment_group); // i am able to print the assignment group. 
 

(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.

 
 
 
 
0 REPLIES 0