- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:14 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:28 AM
GlideAggregate won't directly sort, you will have to enhance your script
this should work
var gr = new GlideAggregate('user');
gr.addEncodedQuery('active=true');
gr.addAggregate('COUNT');
gr.groupBy('category');
gr.query();
var results = [];
while (gr.next()) {
var category = gr.getDisplayValue('category');
var count = gr.getAggregate('COUNT');
results.push({ category: category, count: parseInt(count) });
}
// Sort the results array in descending order by count
results.sort(function(a, b) {
return b.count - a.count;
});
// Print the sorted results
results.forEach(function(result) {
gs.info('category: ' + result.category + ' - ' + result.count);
});
I tried the same script for incident and priority and it worked fine
var gr = new GlideAggregate('incident');
gr.addEncodedQuery('active=true');
gr.addAggregate('COUNT');
gr.groupBy('priority');
gr.query();
var results = [];
while (gr.next()) {
var category = gr.getDisplayValue('priority');
var count = gr.getAggregate('COUNT');
results.push({ category: category, count: parseInt(count) });
}
// Sort the results array in descending order by count
results.sort(function(a, b) {
return b.count - a.count;
});
// Print the sorted results
results.forEach(function(result) {
gs.info('category: ' + result.category + ' - ' + result.count);
});
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:33 AM - edited 04-24-2025 04:34 AM
Hi @Trupti Krishnam
Try below script. It'll sort the output in the descending order.
var gr = new GlideAggregate('sys_user'); //Replace the table name
gr.addEncodedQuery('active=true^department!=NULL'); // replace "Department" with the actual field name
gr.orderByAggregate('COUNT', 'department'); // replace "Department" with the actual field name
gr.addAggregate('COUNT','department');// replace "Department" with the actual field name
gr.groupBy('department');// replace "Department" with the actual field name
gr.query();
while(gr.next()){
gs.info(gr.getDisplayValue('department') + ': ' + gr.getAggregate('COUNT', 'department'));// replace "Department" with the actual field name
}
Output:
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:38 AM
it's show as NAN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 04:39 AM