Auto assign the current incident to the member with the least count of incidents assigned

Shweta Yaksambe
Tera Expert

Hello,

Can someone help me here?

Once the Assignment group is populated, assign the group members as follows:

    1. Check for the group members of that group
    2. Count the number of incidents that are assigned to each of the members.
    3. Assign the current incident to the member with the least count of incidents assigned.

Thanks

3 REPLIES 3

BharathChintala
Mega Sage

@Shweta Yaksambe  Possible with Before BR

 

write before BR on incident table

on insert and update

conditions Assignment group changes

in advanced

 

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

var members = [];
  var gm = new GlideRecord('sys_user_grmember');
  gm.addQuery('group', current.assignment_group);
  gm.query();
  while (gm.next()) {
        members.push(gm.user.toString());
  }
  // 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.addEncodedQuer('active=true^assigned_to!=NULL');
          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, previous);

 

}

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Community Alums
Not applicable

Thank you for the script . working as expected 🙂

Dakota Heath
Tera Contributor

There are different ways to approach this depending on the exact use case. Should it take into account users who are in multiple groups? Should it consider the user's working hours, or vacation time? Without having all of the design details nailed down, we can only offer limited advice. 

But that said, here's the way I would approach it. Assuming that the intention is to track assignment groups separately, I would leverage the sys_user_grmember table. It already has a record for every current user/assignment group combination, and can help you cut down to a single query. On the table, add two new fields: first we need a count of assigned incidents, and second we need a boolean opt-in/opt-out field. This allows you to exempt users from assignment if necessary. 

Incrementing the assigned incidents field could be tackled a number of ways, but a BR on the incident table to increase or decrease it based on the incident's State and Assigned To fields would be one option. From there, another BR could be used to query the sys_user_grmember table when the Assignment Group changes, and locate the user with the fewest assigned incidents. 

Here's a sample BR. Note that this will likely always break to the same user in the event of a tie on the new incident_count field. You could get around this by adding a second orderBy operation with whatever the tie-breaking qualifier should be. 

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', current.getValue('assignment_group');
grMember.addQuery('opt_out', false);
grMember.orderBy('incident_count');
grMember.query();
if (grMember.next()){
    current.setValue('assigned_to', grMember.getValue('user');
}