How to assign the case to user who is having less number of cases.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 02:38 AM
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
(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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 03:28 AM
You are making things more complicated to find out that user, please use below more efficient script,
I did below script for incident table and Service Desk Group, which is tested and working fine.
var members = [];
var gm = new GlideRecord('sys_user_grmember');
gm.addQuery('user.active', true);
gm.addQuery('group', 'd625dccec0a8016700a222a0f7900d06');
gm.query();
while (gm.next()) {
members.push(gm.user.toString());
}
var oldCount = 0,
newCount = 0;
var lowestCountUser = "";
var agg = new GlideAggregate('incident');
agg.addEncodedQuery('assigned_to!=NULL');
agg.addQuery('assignment_group', 'd625dccec0a8016700a222a0f7900d06');
agg.addEncodedQuery('assigned_toIN'+ members);
agg.groupBy("assigned_to");
agg.addAggregate('COUNT',"assigned_to");
agg.query();
while (agg.next()) {
newCount = agg.getAggregate('COUNT',"assigned_to");
if (oldCount > newCount) {
lowestCountUser = agg.assigned_to.toString();
oldCount=newCount;
} else if (oldCount == 0) {
oldCount = agg.getAggregate('COUNT',"assigned_to");
lowestCountUser = agg.assigned_to.toString();
}
}
current.assigned_to = lowestCountUser ;
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();
Script for your table and group sys_id :
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(gm.user.toString());
}
var oldCount = 0,
newCount = 0;
var lowestCountUser = "";
var agg = new GlideAggregate('sn_hr_core_case');
agg.addEncodedQuery('assigned_to!=NULL');
agg.addQuery('assignment_group', '4b88b9631bdaa05068e955b61a4bcba8');
agg.addEncodedQuery('assigned_toIN'+ members);
agg.groupBy("assigned_to");
agg.addAggregate('COUNT',"assigned_to");
agg.query();
while (agg.next()) {
newCount = agg.getAggregate('COUNT',"assigned_to");
if (oldCount > newCount) {
lowestCountUser = agg.assigned_to.toString();
oldCount=newCount;
} else if (oldCount == 0) {
oldCount = agg.getAggregate('COUNT',"assigned_to");
lowestCountUser = agg.assigned_to.toString();
}
}
current.assigned_to = lowestCountUser ;// here we setting required assigned to
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();
Note: This should be before BR without using current.update() method.
Please mark this as Correct or Helpful if it helps.
Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 04:19 AM
Hi Abhijeet,
i'm not able to under this part of your code?
newCount = agg.getAggregate('COUNT',"assigned_to");
if (oldCount > newCount) {
lowestCountUser = agg.assigned_to.toString();
oldCount=newCount;
} else if (oldCount == 0) {
oldCount = agg.getAggregate('COUNT',"assigned_to");
lowestCountUser = agg.assigned_to.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 05:09 AM
First if condition is actually main part where go on finding smaller count till last record and 'else if' part is to set first count and user variable.
Hops script worked for your case as well since it worked for me for incidents. If so, please mark answer as correct.
Let me know in case of any queries.
Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2022 06:45 AM
Hey,
If your issue is resolved then please mark appropriate answer as Correct/helpful so that other can also get benefits by this in future.
Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP