Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Can you order the result from "orderByAggregate('COUNT')" by ascending instead of the default descending?

t_ivanov
Kilo Contributor

Hello. I have a script that counts incidents that have "assigned_to" value, and then groups them by "assigned_to" value.

After that I sort the result with "orderByAggregate", but the default result is sorted by "desc". Is there any possible way to sort the result by "asc", so that I get the lowest value first? Thanks.

var count = new GlideAggregate('incident');
count.addEncodedQuery(aggregateQuery); // Query for specific user/s
count.addAggregate('COUNT');
count.groupBy('assigned_to');
count.orderByAggregate('COUNT');
count.query();

8 REPLIES 8

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

 

count.orderByAggregate('COUNT');  Instead of this use 

agg.orderByAggregate('count','assigned_to');

Thanks,

Ashutosh Munot

I have tried this, but I get this error:

Syntax Error or Access Rule Violation detected by database (Unknown column 'count_of_1741941388' in 'order clause')
 

HI,


See below script which is works:

 

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);
 
}
Thanks,

Ashutosh Munot

I don't see a descending order method. What I'd do is store the results in an array and then sort them that way before printing them out.