Find the highest count of records grouped by category

Trupti Krishnam
Tera Contributor

I want to Find the highest count of records grouped by category .., for example the category is 'book' which has 40 records , Category is Pen that has 80 records , category is 'bag' that has 1000 records , category is 'laptop' = 80 the output should be like

 

category bag - 1000

category laptop  - 80

category pen - 80

category book - 40 

I have written a GlideAggregate Script 

 

var gr = new GlideAggregate('user');
gr.addEncodedQuery('active=true');
gr.addAggregate('COUNT');
gr.groupBy('category');
gr.orderByDesc('category');
gr.query();

while(gr.next()){
    var ga = gr.getAggregate('COUNT');
    gs.info('category:' +gr.getDisplayValue('category') + 'number is :' +ga);
}
 
but it's not sorting in the descending order 
Help me with the sample code
 

 

1 ACCEPTED SOLUTION
6 REPLIES 6

Trupti Krishnam
Tera Contributor

@J Siva thanks! it's working 

 

Great 👍