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

Hey Chuck, my solution was kinda similar. I store them in Array and then I iterate backwards over that array. 

Kinda weird that there is no "asc" ordering 🙂 

 

Thanks! 🙂

If you feel you need it, I invite you to open an enhancement request! Our product managers DO listen.

Enhancement requests: Tell us how you would improve the ServiceNow product 

lss123
Tera Contributor

I found this to work:

var count = new GlideAggregate('table_name');
count.addAggregate('COUNT');
count.groupBy("group_by_field");
count.orderByAggregate('count');
count.query(); 

Prasen Naikele1
Tera Contributor

Hello  t_ivanov, 

You can try.

count.orderByAescAggregate('COUNT');
thank you.