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

And you would use my code to determine the who that person is with the lowest ticket count....


I did the hard (fun) part, man.   Surely you can do a little GlideRecord insert()


Just plug it into your Inbound Action.


Thanks for your support Geoffrey. I will complete the reaming part


Hi harish, we have the same requirement as yours, if you have done with the complete scripting part, can share the script to auto assign incidents to users that helps me alot.


Hi Krishna,



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


hi Harish, i tried with the above mentioned code but unable to assign the tickets to user automatically. can u please the steps you followed so i can check if i am missing anything.