Round Robin Auto-Assignment of Incidents to Agents directly based on availability in Agent Workspace
With help of Advanced Work Assignment, the work can be assigned in the form of 'Most Majority' or 'Last Assigned' as per the Assignment rule specified in the Queue setup but agents available from Agent Workspace needs to accept the Incident requests in order to work on them. The below code is for those who is looking for automated assignment of tickets (Round robin) to agents according to the availability of agent from Agent Workspace(Inbox) when it is assigned to a group so that agents do not have to accept the Incident requests, it is automatically assigned to them to work on it based on their presence.
Created a date/time field named 'u_last_assigned' in sys_user table to determine the last assigned ticket to the agent.
Created a before Insert Business rule on table 'Incident' and condition is 'Assigned to is empty' and below code has been scripted:
var agent = [], agentarr = [],finalAgentArr = []; var group = current.assignment_group; // get the assignment group which is assigned through mapping/ assignment rules
current.assigned_to =assignAgent(); // sys_id of an agent of a particular assigned assignment group
//Created a function function assignAgent() { var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('group', group); //match the group gr.query(); while (gr.next()) { agent.push(gr.user.toString()); //store the sys_id of agents who belong to the group }
for (var i = 0; i < agent.length; i++) {
//Query the awa_agent_presence table where you can track the Agent's availability in Agent Workspace var presence = new GlideRecord('awa_agent_presence'); presence.addQuery('agent', agent[i]); presence.addQuery('current_presence_state', '0b10223c57a313005baaaa65ef94f970'); //put the sy_id of the 'Available' state presence.query(); while (presence.next()) { agentarr.push(presence.agent.toString()); //push only the available agents in an Agent Workspace as shown in image above
}
}for (var j = 0; j < agentarr.length; j++) { //query the User table to get the last_assigned time for the available agents var grUser = new GlideRecord('sys_user'); grUser.addQuery('sys_id', agentarr[j]); grUser.addQuery(); grUser.query(); while (grUser.next()) {finalAgentArr.push({ sys_id: grUser.sys_id.toString(), last_assigned: grUser.u_last_assigned.toString()
});
} }
gs.info('@Automation final array: ' + JSON.stringify(finalAgentArr));
// Sort on last assigned time, return the sys_id of an agentfinalAgentArr.sort(function(a, b) { return (a.last_assigned < b.last_assigned ? -1 : (a.last_assigned > b.last_assigned ? 1 : 0));
});
updateDateTime(finalAgentArr[0].sys_id); // update the last assigned time in sys_user table
return (finalAgentArr[0].sys_id); // return the Assigned_to agent (sys_id).
}
function updateDateTime(assignedTo) { var nowdt = gs.nowDateTime(); var updateDate = new GlideRecord('sys_user'); updateDate.addQuery('sys_id', assignedTo); updateDate.query(); while (updateDate.next()) { updateDate.u_last_assigned = nowdt; updateDate.update(); }
}
Thanks & Regards,
Ankita Sarkar
Labels:
https://www.servicenow.com/community/developer-articles/round-robin-auto-assignment-of-incidents-to-agents-directly/ta-p/2323093
