How to assign the case to user who is having less number of cases.

ST9
Tera Contributor

Hi All,

I have a requirement to Cancel the case if case is assigned to a assignment group= xyz and short description= 'Hire Set by HR Rep higher job group'
but there is a mandatory field called Assigned To, for which i want to assign the case to xyz group member having less number of tickets,

for this i have written a Before BR, please correct me where i am doing mistake

find_real_file.png

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

var members = [];
  var gm = new GlideRecord('sys_user_grmember');
  gm.addQuery('user.active', true);
  gm.addQuery('group', '4b88b9631bdaa05068e955b61a4bcba8');
  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('sn_hr_core_case');
	agg.addEncodedQuer('active=true^assigned_to!=NULL');
          agg.addQuery('assignment_group', '4b88b9631bdaa05068e955b61a4bcba8');
          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
  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.state= 7;
current.u_resolution_category_tier_1='BPO Team';
current.u_resolution_category_2= 'Request Cancelled';
current.u_resolution_category_3= 'No Activities Performed';
current.u_resolution_notes="No action needed at BPO's end, Hence cancelling this ticket.";
current.update();

})(current, previous);
8 REPLIES 8

suvro
Mega Sage
Mega Sage

Go for Advanced Work Assignments 

Here you will have options to select criterias for assigniing cases

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/administer/advanced-work-assignment/concept/awa-overview.html

 

Mark my response as correct and helpful, if it resolved your query

Thanks and Regards,

Chandra Suvro Bose

ST9
Tera Contributor

We are not using AWA and workspace, we want to do without that

suvro
Mega Sage
Mega Sage
(function executeRule(current, previous /*null when async*/) { 
var count = []; 
// Get a list of their open ticket counts 
var index = 0; 
for (var i = 0; i < members.length; i++) { 
agg = new GlideAggregate('sn_hr_core_case');
 agg.addEncodedQuer('active=true^assigned_to!=NULL'); 
agg.addQuery('assignment_group', '4b88b9631bdaa05068e955b61a4bcba8'); 
agg.addAggregate('assigned_to','COUNT'); 
agg.orderBy('assigned_to'); 
agg.query(); 
while (agg.next()) {
var assignedToCount = {}; 
assignedToCount.count = agg.getAggregate('assigned_to','COUNT'); 
assignedToCount.assignedTo = agg.assigned_to.toString(); 
count.push(assignedToCount);
 } // find the minimum count and store its index
 var min = 0; var max = 0; 
for (var j = 1; j < counts.length; j++) {
 if (counts[j].count < min) { 
min = parseInt(counts[j].count); 
index = parseInt(j); } 
} // get the member with the lowest count
 var userID = counts[index].assignedTo; 
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.state= 7; 
current.u_resolution_category_tier_1='BPO Team';
 current.u_resolution_category_2= 'Request Cancelled';
 current.u_resolution_category_3= 'No Activities Performed'; 
current.u_resolution_notes="No action needed at BPO's end, Hence cancelling this ticket.";
 current.update();
 })(current, previous);

(function executeRule(current, previous /*null when async*/) { var count = []; // Get a list of their open ticket counts var index = 0; for (var i = 0; i < members.length; i++) { agg = new GlideAggregate('sn_hr_core_case'); agg.addEncodedQuer('active=true^assigned_to!=NULL'); agg.addQuery('assignment_group', '4b88b9631bdaa05068e955b61a4bcba8'); agg.addAggregate('assigned_to','COUNT'); agg.orderBy('assigned_to'); agg.query(); while (agg.next()) { var assignedToCount = {}; assignedToCount.count = agg.getAggregate('assigned_to','COUNT'); assignedToCount.assignedTo = agg.assigned_to.toString(); count.push(assignedToCount); } // find the minimum count and store its index var min = 0; var max = 0; for (var j = 1; j < counts.length; j++) { if (counts[j].count < min) { min = parseInt(counts[j].count); index = parseInt(j); } } // get the member with the lowest count var userID = counts[index].assignedTo; 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.state= 7; current.u_resolution_category_tier_1='BPO Team'; current.u_resolution_category_2= 'Request Cancelled'; current.u_resolution_category_3= 'No Activities Performed'; current.u_resolution_notes="No action needed at BPO's end, Hence cancelling this ticket."; current.update(); })(current, previous);

suvro
Mega Sage
Mega Sage

(function executeRule(current, previous /*null when async*/) { 
var count = []; 
// Get a list of their open ticket counts 
var index = 0; 
for (var i = 0; i < members.length; i++) { 
agg = new GlideAggregate('sn_hr_core_case');
 agg.addEncodedQuer('active=true^assigned_to!=NULL'); 
agg.addQuery('assignment_group', '4b88b9631bdaa05068e955b61a4bcba8'); 
agg.addAggregate('assigned_to','COUNT'); 
agg.orderBy('assigned_to'); 
agg.query(); 
while (agg.next()) {
var assignedToCount = {}; 
assignedToCount.count = agg.getAggregate('assigned_to','COUNT'); 
assignedToCount.assignedTo = agg.assigned_to.toString(); 
count.push(assignedToCount);
 } // find the minimum count and store its index
 var min = 0; var max = 0; 
for (var j = 1; j < counts.length; j++) {
 if (counts[j].count < min) { 
min = parseInt(counts[j].count); 
index = parseInt(j); } 
} // get the member with the lowest count
 var userID = counts[index].assignedTo; 
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.state= 7; 
current.u_resolution_category_tier_1='BPO Team';
 current.u_resolution_category_2= 'Request Cancelled';
 current.u_resolution_category_3= 'No Activities Performed'; 
current.u_resolution_notes="No action needed at BPO's end, Hence cancelling this ticket.";
 current.update();
 })(current, previous);