The CreatorCon Call for Content is officially open! Get started here.

Auto Assignment of new incidents and tasks(Queue Management- Automation)-Round Robin

kulkarnivivekn
Tera Contributor

I have requirement to to create a round-robin type approach (Auto Assignment) for any incidents/tasks that come into our  groups(Many groups). 

I have created below BR as per one of the community suggested and its working fine for one group.

(function executeRule(current, previous /*null when async*/ ) {

gs.sleep(3000); // Sleep statement added to prevent ticket flood being assigned to a single technician.
current.assigned_to = resolveTech();

function resolveTech() {
var tech_arr = [];
var group = new GlideRecord('sys_user_grmember');
group.addQuery('group', '0a52d3dcd7011200f2d224837e6103f2');

group.query();
var tech = "";
var lastAssigned = "";
var isAssignable = false;
while (group.next()) {
isAssignable = group.user.u_round_robin_active;
tech = group.user.sys_id;
lastAssigned = group.user.u_last_ticket_assigned + "";

if (isAssignable == true) {
tech_arr.push({
sys_id: tech,
last_assigned: lastAssigned
});
}
}
// Sort on last assigned, return user with.

tech_arr.sort(function(a, b) {
return (a.last_assigned < b.last_assigned ? -1 : (a.last_assigned > b.last_assigned ? 1 : 0));
});
updateDateTime(tech_arr[0].sys_id); // Updating ticket last assigned date
gs.sleep(2000);
return (tech_arr[0].sys_id); // Assigned_to technician (sys_id).
}

function updateDateTime(assignedTo_sysID) {
var nowdt = gs.nowDateTime();
var updateDate = new GlideRecord('sys_user');
updateDate.addQuery('sys_id', assignedTo_sysID);
updateDate.query();
while (updateDate.next()) {
updateDate.u_last_ticket_assigned = nowdt;
updateDate.update();
}
}

})(current, previous);

If I add another group's sys_id in code then its not working for other group(i mean its not assigning to other group members).And i have below requirement.

1.Based on User Time zone field , ticket should assign to user(Lets we have 6 members,three from India and three from US, When its off hours in India then it will assign to US time zone members and it should exclude)
2.Ticket should assign to particular users in case P1/P2(Need to hardcode the users name)
3.Need to check Active(user.u_round_robin_active) and last login time of the user before assigning ticket(Both condition should match to assign ticket)

 

Please help me to automate this approach.

 

Thanks In Advance

1 ACCEPTED SOLUTION

Baala T
Mega Guru

Hi,

I tried this way, it works fine with Round Robin concept. This might not be the final code, probably you could enhance with other use case.


gs.sleep(3000); 

//Default P1, P2 handling Users
var aUser = ['62826bf03710200044e0bfc8bcbe5df1', 'a8f98bb0eb32010045e1a5115206fe3a']; //Abel Tuter, Abraham Lincoln

//The ticket should be assigned to based on Group membership
if(current.priority > 2){
	//Fetch list of Users
	var grGrpMem = new GlideRecord('sys_user_grmember');
	grGrpMem.addQuery('group', current.assignment_group);
	grGrpMem.query();

	while (grGrpMem.next()) {
		aUser.push(grGrpMem.getValue('user'));
	}
}

//Fetch right User
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', 'IN', aUser.join()); //Fetch all the Users in the group
grUser.addQuery('u_round_robin_active', true); //Filter only eligible for Round Robin
//grUser.addEncodedQuery('last_login_timeONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()'); //Who is available in the Shift
grUser.orderBy('u_last_ticket_assigned'); //Order by ASC on Last Ticket Assigned
grUser.setLimit(1); //Fetch only the first user
grUser.query();

if (grUser.next()) {
    grUser.setValue('u_last_ticket_assigned', gs.nowDateTime());
    grUser.update();

    current.setValue('assigned_to', grUser.getValue('sys_id'));
}

 

Hope this might helpful to you!

 

Regards,
Bala T

View solution in original post

8 REPLIES 8

kulkarnivivekn
Tera Contributor

@xiaix  : Please help to do this automation since you have provided solution earlier and i have followed same.

Baala T
Mega Guru

Hi,

I tried this way, it works fine with Round Robin concept. This might not be the final code, probably you could enhance with other use case.


gs.sleep(3000); 

//Default P1, P2 handling Users
var aUser = ['62826bf03710200044e0bfc8bcbe5df1', 'a8f98bb0eb32010045e1a5115206fe3a']; //Abel Tuter, Abraham Lincoln

//The ticket should be assigned to based on Group membership
if(current.priority > 2){
	//Fetch list of Users
	var grGrpMem = new GlideRecord('sys_user_grmember');
	grGrpMem.addQuery('group', current.assignment_group);
	grGrpMem.query();

	while (grGrpMem.next()) {
		aUser.push(grGrpMem.getValue('user'));
	}
}

//Fetch right User
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', 'IN', aUser.join()); //Fetch all the Users in the group
grUser.addQuery('u_round_robin_active', true); //Filter only eligible for Round Robin
//grUser.addEncodedQuery('last_login_timeONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()'); //Who is available in the Shift
grUser.orderBy('u_last_ticket_assigned'); //Order by ASC on Last Ticket Assigned
grUser.setLimit(1); //Fetch only the first user
grUser.query();

if (grUser.next()) {
    grUser.setValue('u_last_ticket_assigned', gs.nowDateTime());
    grUser.update();

    current.setValue('assigned_to', grUser.getValue('sys_id'));
}

 

Hope this might helpful to you!

 

Regards,
Bala T

@baalat  : Thank you so much for the response and its working fine but still its not working as expected for below requirement.

1.Priority : Working fine if we mentioned user and if the mentioned user is not present in one group then its assigned to the mentioned user from the other group.

Lets Say  I have added this Round Robin for two Groups - A and B.

Albert and Abreham is part for Group A But From the Group B i haven't added any users sys_id in code.

Now if i assign any P1/P2 ticket to Group B then its Assigning to  Albert or Abreham who is part for Group A not B.

2.Based on User Time zone field , ticket should assign to user(Lets we have 6 members,three from India and three from US, When its off hours in India then it will assign to US time zone members and it should exclude Indian time zone users).

3.Its working fine for All groups and last login and active user.

 

Thanks And Regards,

Vivek

Hi,

Good to hear that it helped a bit.

Added two tables and modified the code.

GrMem Critical Support

GroupUser
Group AUser A
Group AUser B
Group BUser A
Group BUser C
Group CUser D

 Timezone Support

TimezoneStartEnd
Asia/Kolkata2020-08-13 04:30:002020-08-13 13:30:00
America/New_York2020-08-13 14:00:002020-08-13 23:00:00
Pacific/Auckland2020-08-13 22:00:002020-08-14 07:00:00

I considered 9 AM to 6 PM as the working hours on each timezone, the same value converted in GMT and stored in the above table. This should get updated every day 12:00 AM GMT.

Based on these, the logic should be as below 

gs.sleep(3000);

var aUser = [];
var aTimeZone = [];

//The ticket should be assigned to based on Group membership
if (current.priority <= 2) {
	//Fetch list of Users who supports the Priority Tickets
	/*
	* Table Name : u_grmem_critical_support
	* Fields:
	*	Group-u_group-reference-sys_user_group
	*	User-u_user-reference-sys_user
	*/
	var grCriMem = new GlideRecord('u_grmem_critical_support');
	grCriMem.addQuery('u_group', current.assignment_group);
	grCriMem.query();
	while (grCriMem.next()) {
		aUser.push(grCriMem.getValue('u_user'));
	}
}

if(aUser.length == 0){
	//Fetch list of Users
	var grGrpMem = new GlideRecord('sys_user_grmember');
	grGrpMem.addQuery('group', current.assignment_group);
	grGrpMem.query();
	while (grGrpMem.next()) {
		aUser.push(grGrpMem.getValue('user'));
	}
}

//Fetch Eligible Timezone
/*
* Table Name : u_timezone_support
* Fields:
*	TimeZone-u_timezone-string-(choice value similar to User.Timezone values)
*	Start-u_start-datetime (value should be converted in UTC and stores with current date)
*	End-u_end-datetime (value should be converted in UTC and stores with current date)
* Sample Data:
*	Asia/Kolkata->2020-08-13 04:30:00->2020-08-13 13:30:00
*	America/New_York->2020-08-13 14:00:00->2020-08-13 23:00:00
*	Pacific/Auckland->2020-08-13 22:00:00->2020-08-14 07:00:00
*/
var grZone = new GlideRecord('u_timezone_support');
grZone.addQuery('u_end', '>', gs.nowNoTZ());
grZone.addQuery('u_start', '<', gs.nowNoTZ());
grZone.query();

while(grZone.next()){
	aTimeZone.push(grZone.getValue('u_timezone'));
}

//Fetch right User
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', 'IN', aUser.join()); //Fetch all the Users in the group
grUser.addQuery('time_zone', 'IN', aTimeZone.join()); //Fetch all the Users in the group
grUser.addQuery('u_round_robin_active', true); //Filter only eligible for Round Robin
grUser.orderBy('u_last_ticket_assigned'); //Order by ASC on Last Ticket Assigned
grUser.setLimit(1); //Fetch only the first user
grUser.query();

if (grUser.next()) {
    grUser.setValue('u_last_ticket_assigned', gs.nowDateTime());
    grUser.update();

    current.setValue('assigned_to', grUser.getValue('sys_id'));
}

 

Regards,
Bala T