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.

How to get category with maximum and minimum incidents using glideaggregate

Vidya Shree
Kilo Sage

Hi All,

 

I need help to get the category name and count which has maximum and minimum incidents. We can get the count of incidents under all the categories but here i want only max and min count and category.

 

Thanks,

Vidyashree

6 REPLIES 6

Sid_Takali
Kilo Patron

Hi @Vidya Shree Make group by Category field in Incident List View. You will get count of incidents by Category.

SiddharamTakali_0-1716466533378.png

 

Regards,

Sid

 

Hi @Sid_Takali 

 

Thank you for your reply.

I looking for script using GlideAggregate which returns only Maximum and Minimum record count category.

Community Alums
Not applicable

Hi @Vidya Shree ,

Please refer below script 

var incidentGA = new GlideAggregate('incident');
incidentGA.addAggregate('COUNT', 'category');
incidentGA.addAggregate('MAX', 'category');
incidentGA.addAggregate('MIN', 'category');

incidentGA.query();

while (incidentGA.next()) {
gs.print('CATEGORY: ' + incidentGA.getValue('category'));
gs.print('AVG: ' + incidentGA.getAggregate('AVG', 'category'));
gs.print('COUNT: ' + incidentGA.getAggregate('COUNT', 'category'));
gs.print('MAX: ' + incidentGA.getAggregate('MAX', 'category'));
gs.print('MIN: ' + incidentGA.getAggregate('MIN', 'category'));
gs.print(' ');
}

Result 

SarthakKashyap_0-1716469365017.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

 

Community Alums
Not applicable

@Vidya Shree ,

You can also check script as well 

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.groupBy('category');
count.query();
gs.info(count.getRowCount());
while (count.next()) {
    var min = count.getAggregate('MIN', 'sys_mod_count');
    var max = count.getAggregate('MAX', 'sys_mod_count');
    var category = count.category.getDisplayValue();
    gs.info(category + " Update counts: MIN = " + min + " MAX = " + max);
}

Result 

SarthakKashyap_0-1716469613461.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak