GlideAggregate GroupBy() - Get records for each group by value

Abbey2
Tera Expert

Hi All,

When we use GlideAggregate to group by, i know it possible to get a count against a grouped by field.

But rather than getting a count, can i get a return of those values. For example on asset table, group by assigned to user (to get distinct user), and return all the asset name for each distinct assigned to user.

 

var assigned_users = [];
var asset = new GlideAggregate('alm_hardware');

asset.addQuery("sys_updated")<=javascript:gs.daysAgoStart(1)");
asset.addQuery("class", "Computer");
asset.groupBy("assigned_to");
asset.query();
while (asset.next()) {
        assigned_users.push(asset.assigned_to.getRefRecord()); //this gets the assigned to user reference in an array
        
**********<how to get all asset records(name or assettag) for each assigned to user>*********

}
6 REPLIES 6

sbh
Tera Guru

I think you can create a database view joining the alm_hardware and sys_user tables and query that. Each record will have data from both tables.

hunterphillips
Tera Contributor

 

You can use .setGroup(false) to return each row (instead of a group) and the rows will be sorted by the grouping

 

var ga = new GlideAggregate('incident');
ga.groupBy('assigned_to');
ga.setGroup(false);

/*
- Incident1 Abel Tutor
- Incident2 Abel Turor
- Incident3 David Loo 
- Incident4 David Loo
...
*/