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

Hello Harish,



Thanks for your response. Code is working, but its again assigning to single user. Can you plz through some light and elaborate me little bit deep. You can modify my code or code gave by you and help me out. so that to assign tickets to all users in group in round-robin fashion.




Thanks,


Nitesh


Hii Harish,



Thanks for Your suggestions. Its working Fine with Incident Table, But If I created any user defined table and working on it, All tickets are again assigning to single user. Is it possible to do it in user defined table.



Thanks,


Nitesh


Hi harishdasari,

It's working fine thanks, i need to assign the ticket based on caller languge and least ticket count in the group member.any idea?

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);


  }