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.

Script to get list of incidents based on categories

Souvick A
Tera Contributor

Hi Team,

I have a requirement to show the number of incidents based on categories. Can anyone help me with the script to show these records as it is below:

Hardware :12

Software : 3

Database : 23

Inquiry/Help : 40

 

Regards

Souvick

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Souvick A 

Please use below script:

var incidents = new GlideAggregate('incident');
incidents.addAggregate('count', 'category'); // Add an aggregate to count based on category field
incidents.orderBy('category');
incidents.query();

while (incidents.next()) {
// Get the current category and count
var category = incidents.category;
var count = incidents.getAggregate('count', 'category');

gs.info('Category: ' + category + ' Count: ' + count);
}

Best Regards
Aman Kumar

View solution in original post

7 REPLIES 7

Aman Kumar S
Kilo Patron

Hi @Souvick A 

Please use below script:

var incidents = new GlideAggregate('incident');
incidents.addAggregate('count', 'category'); // Add an aggregate to count based on category field
incidents.orderBy('category');
incidents.query();

while (incidents.next()) {
// Get the current category and count
var category = incidents.category;
var count = incidents.getAggregate('count', 'category');

gs.info('Category: ' + category + ' Count: ' + count);
}

Best Regards
Aman Kumar

Thank you Aman 

Hi I need to display the incident numbers as well, could anyone help?? Like if software category has 4 incidents under it I have to display it like
Category software count 4
INC0001

INC0002

INC0003
INC0004

Hello @Aman Kumar S ,

Even I am also looking for the script to get the count of category and the incident numbers of that each as well. Can anyone help?

Thanks