GlideAggregate - orderByAggregate(String agg, String name)

Orders the aggregates based on the specified aggregate and field.

Table 1. Parameters
Name Type Description
agg String Type of aggregation, for example SUM, COUNT, MIN, MAX.
fieldName String Name of the field to aggregate.
Table 2. Returns
Type Description
void

Scoped equivalent

To use the orderByAggregate() method in a scoped application, use the corresponding scoped method: Scoped GlideAggregate - orderByAggregate(String agg, String fieldName).

var agg = new GlideAggregate('incident');
agg.addAggregate('count','category'); 
agg.orderByAggregate('count', 'category'); 
agg.orderBy('category'); 
agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
agg.query(); 
while (agg.next()) { 
  var category = agg.category;
  var count = agg.getAggregate('count','category');
  var query = agg.getQuery();  
  var agg2 = new GlideAggregate('incident');   
  agg2.addAggregate('count','category');
  agg2.orderByAggregate('count', 'category');
  agg2.orderBy('category');
  agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
  agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
  agg2.addEncodedQuery(query);
  agg2.query();
  var last = "";
  while (agg2.next()) {
     last = agg2.getAggregate('count','category');      
  }
  gs.log(category + ": Last month:" + count + " Previous Month:" + last);
 
}