- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-10-2022 01:42 AM
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 agent
finalAgentArr.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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @Ankita Sarkar ,
Last assigned field not updating after assigning incident. Can you please help on this.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I know this is a bit old but I stumbled on this looking for something else...
In case anybody else does, be aware that ServiceNow added native auto-assignment to AWA a few releases ago (Utah I think?) - no custom code needed 🙂
When creating/updating an Assignment Rule, just set "Enable auto-assign work items" = true.