Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

assign a incident automatically to a user in group

harishdasari
Tera Guru

Hi,

I have a requirement, when   inbound email action is triggered and created a incident in servicenow, automatically it has to assign incident to a person in the group based upon his workload. For example if 2 persons are in a group and one person is working on 2 incidents and another person don't have any incidents to work, then automatically it has to assign incident to this person.

Can anyone let me, how can achieve this.

Thanks

13 REPLIES 13

Inactive_Us1474
Giga Guru

In inbound mail action you can create a glide query in script and assign it to user as per this use case.



Thanks


Akhil


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You may want to check out this solution in Share:


ServiceNow Share


Geoffrey2
ServiceNow Employee
ServiceNow Employee

I love coding challenges



In my instance I have the following open Incident by User.   You want the code to tell you the person with the lowest ticket count. So that's Beth Anglin (the are only 4 memebers - nobody has 0)


Screen Shot 2016-09-15 at 10.23.43 PM.png



  var groupName = 'Service Desk';



  // 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.name', 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.name', 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];



  // Log their name to verify


  var user = new GlideRecord('sys_user');


  if (user.get(userID)) {


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


  }



Result:


Screen Shot 2016-09-15 at 10.27.29 PM.png


Hi Geoffrey,



Thanks for your reply and but actual requirement is not to count the users with lowest count. Requirement is a incident will be created using inbound email action and service-now has to automatically assign the incident based upon person with the lowest ticket count.


Hope you are clear I believe.