script to assign tickets for users in a group with active tickets count is minimum to maximum
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2017 12:47 AM
hi, we have a requirement to run a schedule job everyday at 06 pm and should call a script include which queries all the active tickets in a customized table and assign to the users in a group. the tickets should be automatically assigned in such a way that it should assign to per with least active tickets assigned to him and should continue assigning tickets in incremental order of ticket count of the user.(say out of 20 active tickets with 15 users, first ticket should be assigned to user with 0 active tickets, next 1, 2, 3 active tickets of the user. please help me with the script.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2017 02:38 AM
Hey Krishna,
I was able to modify the codes.
Kindly check the code and let me know whether it is working fine for you.
var groupName = 'CAB Approval';
// 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));
}
var counts = [], agg, count;
for (var i = 0; i < members.length; i++) {
count = 0;
agg = new GlideAggregate('change_request');
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);
}
var a= new GlideRecord('change_request');
a.addQuery('state',1);
a.query();
while(a.next())
{
if(a.assigned_to==''){
current.assigned_to=userID;
current.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2017 02:07 AM
hi swathi, i also checked with similar script(previously) and also with the above mentioned script but not getting desired output... logic n script everything seems to be fine but still something is wrong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-17-2017 02:10 AM
Hi swathi, this is the script i tried previously. check if u can alter to get accurate output.
Scheduled jobs:
var record = new SDScript();
record.updateIncident();
Script include:
var SDScript = Class.create();
SDScript.prototype = {
initialize: function() {
},
updateIncident : function(query)
{
// Get a list of members for this group
var groupName = 'Service Desk';
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('shopping');
agg.addActiveQuery();
agg.addQuery('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
var min = counts[0], 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 and assign to users
var userID = members[index];
for (var k = index; k < counts.length; k++) {
var inc = new GlideRecord('shopping');
inc.addEncodedQuery(query);
inc.query();
while(inc.next())
{
inc.assigned_to = group.userID[k];
inc.update();
}
}
},
type: 'SDScript'
};