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

Hi @imranmotad,

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.getDisplayValue(); // Get display value of category field
    var count = incidents.getAggregate('count', 'category');

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

    // Now, if you want to get incident numbers for each category, you can add another query
    var incidentNumbers = [];
    var incidentGr = new GlideRecord('incident');
    incidentGr.addQuery('category', category); // Query for incidents with the current category
    incidentGr.query();
    while (incidentGr.next()) {
        incidentNumbers.push(incidentGr.number.getDisplayValue()); // Get display value of incident number
    }

    gs.info('Incident Numbers for Category ' + category + ': ' + incidentNumbers.join(', '));
}

 

Thanks
SP

You can try this.

 

 var gr_inc = new GlideAggregate("incident");
 gr_inc.addAggregate("count","category");
 gr_inc.orderBy("category");
 gr_inc.query();
 while(gr_inc.next())
 {
    var category = gr_inc.category;
    var count = gr_inc.getAggregate("count","category");
    gs.print("Categort :" + category + " Count: " + count);
    var gr_incident = new GlideRecord("incident");
    gr_incident.addQuery("category",category);
    gr_incident.query();
    while(gr_incident.next())
    {
    gs.print(gr_incident.number);
    }
 }
Please mark helpful and accept the soultion.

var incidentGR = new GlideAggregate('incident');
incidentGR.addNotNullQuery('category');
incidentGR.groupBy('category');
incidentGR.query();
while (incidentGR.next()) {
var category = incidentGR.getValue('category');
gs.print('Category: ' + category + '\n');
var incGR = new GlideRecord('incident');
incGR.addQuery('category', category);
incGR.query();
while (incGR.next()) {
gs.print(' - ' + incGR.getValue('number'));
}
}