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.

Display Category and count and the list of incidents in the category

Srimathi PM1
Giga Expert

Hi I need to display the categories and categories count incident numbers under each category, could anyone help?? Like if software category has 4 incidents under it I have to display it like.. Thanks in Advance
Category software count 4
INC0001

INC0002

INC0003
INC0004

1 ACCEPTED SOLUTION

Srimathi PM1
Giga Expert

Here is the code 

var inc = new GlideAggregate('incident');
inc.addEncodedQuery('active=true');
inc.addAggregate('COUNT', 'category');
inc.orderBy('category');
inc.query();

while(inc.next()){
gs.print(inc.category + " " + inc.getAggregate('COUNT', 'category'));

var number = new GlideRecord('incident');
number.addEncodedQuery('active=true^category=' + inc.category);
number.query();

while (number.next()){
gs.print(number.number);
}
}

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Srimathi,

Developer link has all of relevant examples. Please check it out. In case, if not follow link for a check.

Srimathi PM1
Giga Expert

Thanks I got the solution šŸ™‚ 

Srimathi PM1
Giga Expert

Here is the code 

var inc = new GlideAggregate('incident');
inc.addEncodedQuery('active=true');
inc.addAggregate('COUNT', 'category');
inc.orderBy('category');
inc.query();

while(inc.next()){
gs.print(inc.category + " " + inc.getAggregate('COUNT', 'category'));

var number = new GlideRecord('incident');
number.addEncodedQuery('active=true^category=' + inc.category);
number.query();

while (number.next()){
gs.print(number.number);
}
}

rk_in
Tera Contributor

var categoryCounts = {};

var gr = new GlideRecord('incident');
gr.query();

while (gr.next()) {
var category = gr.getValue('category');
var incidentNumber = gr.getValue('incident_number');

if (!categoryCounts[category]) {
categoryCounts[category] = {
count: 0,
incidents: []
};
}

categoryCounts[category].count++;
categoryCounts[category].incidents.push(incidentNumber);
}

for (var category in categoryCounts) {
var categoryData = categoryCounts[category];
gs.info('Category: ' + category + ' - Incident Count: ' + categoryData.count);
gs.info('Incidents: ' + categoryData.incidents.join(', '));
}