How to assign tickets automatically to "Assigned To" field in Helsinki

niteesh
Kilo Contributor

Hello,

By Referencing Round-Robin (Auto Assignment of new incidents and tasks) , I am assigning tickets automatically, But All tickets are assigning to single person. can anyone make changes, so that, Assign to all persons and in Round-Robin Procedure.

find_real_file.png

Thanks,

NItesh

1 ACCEPTED SOLUTION

harishdasari
Tera Guru

Hi Nitesh,



Create On-After business rule and apply this code



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



  var groupName = current.assignment_group;



  // Get a list of members for this group


  var members = [];


  var gm = new GlideRecord('sys_user_grmember');


  gm.addQuery('user.active', true);


  gm.addQuery('group', groupName);


  gm.query();


  while (gm.next()) {


          members.push(String(gm.user));


  }



  // Get a list of their open ticket counts


  var counts = [], agg, count;


  for (var i = 0; i < members.length; i++) {


          count = 0;


          agg = new GlideAggregate('incident');


          agg.addActiveQuery();


          agg.addQuery('assignment_group', groupName);


          agg.addQuery('assigned_to', members[i]);


          agg.addAggregate('COUNT');


          agg.query();


          if (agg.next())


                  count = agg.getAggregate('COUNT');


          counts.push(count);


  }



  // find the minimum count and store its index


  // we cannot use .sort().shift() or we won't know where to look in the members array


  var min = counts[0];


  var index = 0;


  for (var j = 1; j < counts.length; j++) {


          if (counts[j] < min) {


                  min = parseInt(counts[j]);


                  index = parseInt(j);


          }


  }



  // get the member with the lowest count


  var userID = members[index];


  var h;


  // Log their name to verify


  var user = new GlideRecord('sys_user');


  if (user.get(userID)) {


    gs.log('Name: ' + user.sys_id);


  h = user.sys_id;  


  }  


current.assigned_to = h;


current.update();


})(current, previous);




Thanks and hope this is helpful


View solution in original post

7 REPLIES 7

harishdasari
Tera Guru

Hi Nithesh,



I have changed some code and test it.. Actually it will check for the user who has less number of incidents in the group and it will be assigned to the user in that group based on their workload.



  var groupName = current.assignment_group;  


 


  // Get a list of members for this group  


  var members = [];  


  var gm = new GlideRecord('sys_user_grmember');  


  gm.addQuery('user.active', true);  


  gm.addQuery('group', groupName);  


  gm.query();  


  while (gm.next()) {  


          members.push(String(gm.user));  


  }  


 


  // Get a list of their open ticket counts  


  var counts = [], agg, count;  


  for (var i = 0; i < members.length; i++) {  


          count = 0;  


          agg = new GlideAggregate('incident');  


          agg.addActiveQuery();  


          agg.addQuery('assignment_group', groupName);  


          agg.addQuery('assigned_to', members[i]);  


          agg.addAggregate('COUNT');  


          agg.query();  


          if (agg.next())  


                  count = agg.getAggregate('COUNT');  


          counts.push(count);  


  }  


 


  // find the minimum count and store its index  


  // we cannot use .sort().shift() or we won't know where to look in the members array  


  var min = counts[0];  


  var index = 0;  


  for (var j = 1; j < counts.length; j++) {  


          if (counts[j] < min) {  


                  min = parseInt(counts[j]);  


                  index = parseInt(j);  


          }  


  }  


 


  // get the member with the lowest count  


  var userID = members[index];  


  var h;


  // Log their name to verify  


  var user = new GlideRecord('sys_user');  


  if (user.get(userID)) {  


 


          gs.log('Name: ' + user.sys_id);  


  h = user.sys_id;


   


  }    


current.assigned_to = h;


current.update();



// assign this user to the assigned to field


  var queue = new GlideRecord('incident');


  queue.addQuery('active',true);


  queue.addQuery('assigned_to', '');


  queue.query();


  if(queue.next())


  {


  queue.assigned_to = user.sys_id;


  queue.update();


  gs.log('user has been assigned to incident' + user.name);


  }


Awesome Harish.. Its working.. Thanks for your patience and support. You saved a lot of time for me,,


Keep Rocking..



Thanks,


Nitesh


is it possible to check shift of engineer additionally in this script.?