Need help with aggregate script

robhaas
Tera Contributor

I am attempting to populate a field fields on a table using GlideAggregate. I have successfully managed to populate 1 field, but am not looking to populate others. The idea here is to find all users with a specific department number and then count the amount of incidents open/closed/etc... Here is what I have to calculate closed yesterday. I am only using 1 specific user right now for testing.

**********************************************************************************

var usr = new GlideRecord('sys_user');

usr.addQuery('sys_id', '0a9e1a1a4fc89e00c0cba3928110c7a8');

usr.query();

var person = usr.sys_id;

while (usr.next()){

var inc = new GlideAggregate('incident');

inc.addQuery('closed_at', '>=', gs.beginningOfYesterday());

inc.addQuery('closed_at', '<', gs.beginningOfToday());

inc.addQuery('assigned_to', person);

inc.addAggregate('COUNT');

inc.query();

var num = 0;

if(inc.next()){

num = inc.getAggregate('COUNT');

var stats = new GlideRecord('u_stats');

stats.addQuery('u_analyst.sys_id', person);

stats.query();

if (stats.next()){

stats.u_closed_yesterday = num;

stats.update();

}

}

};

**********************************************************************************

I want to also count open incidents and put that in the field u_open on the stats table. So what I need help with, is how do I cleanly pass the variable 'person' to a new function and run an aggregate? For instance, if this is the script to count open incidents, I just want to set the 'person' query as global and use it in multiple functions.

**********************************************************************************

var inc = new GlideAggregate('incident');
inc.addQuery('state', 'IN', '1,10,11,12,20');
inc.addQuery('assigned_to', person);
inc.addAggregate('COUNT');
inc.query();

var open = 0;

if(inc.next()){
open = inc.getAggregate('COUNT');

var stats = new GlideRecord('u_stats');
stats.addQuery('u_analyst.sys_id', person);
stats.query();

if (stats.next()){
stats.u_open = open;
stats.update();
}
}
}

}

**********************************************************************************

Hopefully my question makes sense. If not, I will do my best to explain it better. Thanks for all help in advance!

1 ACCEPTED SOLUTION

Hi Robert,



Thanks for the clarification. To find all users in a specific department...




// Get all users with that department...


var user = new GlideRecord('sys_user');


user.addQuery('department.name', '2112');


user.query();



while (user.next()) {


        // call your function here, passing the user's sys_id


        function2(user.getValue('sys_id'));


}



Does that help?


View solution in original post

13 REPLIES 13

I was referencing 'function2' from above to do the counting and saving.


Ok, got it I believe! I will work on this a bit more and let you know. Thanks again!


I have it working now. Thank you sir!


Glad to hear you go it working. Thanks for using the community!