Incident Management

ServiceNow40
Tera Contributor

1. To print total number of incident like it's "Active/inactive".

2.To print category = Inquiry / help & Software & Hardware & Network & Database related incident need to count.

 

Any idea these codes help me.

4 REPLIES 4

Voona Rohila
Kilo Patron
Kilo Patron

Hi @ServiceNow40 

Refer below link, which has the code for your requirement

https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server/no-namespace/c_GlideAggregateS...

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category'); 
agg.orderBy('category'); 
agg.query(); 
while (agg.next()) { 
  var category = agg.category;
  var count = agg.getAggregate('count', 'category');
  gs.info(category + ": Current number of incidents:" + count);
}

Replace 'Category' with 'active' in above code, You will get the result accordingly for your first req too.

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'active'); 
agg.orderBy('active'); 
agg.query(); 
while (agg.next()) { 
  var category = agg.active;
  var count = agg.getAggregate('count', 'active');
  gs.info(category + ": Current number of incidents:" + count);
}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

SufiyanMurshad
Tera Contributor

Hi , 

You can use below script for total numbers of active/inactive incidents in Background script

script: 

(function countIncidentsByStatus() {
    var activeCount = new GlideAggregate('incident');
    activeCount.addAggregate('COUNT');
    activeCount.addQuery('active', true);
    activeCount.query();
    if (activeCount.next()) {
        gs.print("Total Active Incidents: " + activeCount.getAggregate('COUNT'));
    }

    var inactiveCount = new GlideAggregate('incident');
    inactiveCount.addAggregate('COUNT');
    inactiveCount.addQuery('active', false);
    inactiveCount.query();
    if (inactiveCount.next()) {
        gs.print("Total Inactive Incidents: " + inactiveCount.getAggregate('COUNT'));
    }
})();
 
 
Below script for To print category = Inquiry / help & Software & Hardware & Network & Database
Script:
(function countIncidentsByCategory() {
    var categories = ['inquiry/help', 'software', 'hardware', 'network', 'database'];
    var categoryCounts = {};

    categories.forEach(function(category) {
        var incidentCount = new GlideAggregate('incident');
        incidentCount.addAggregate('COUNT');
        incidentCount.addQuery('category', category);
        incidentCount.query();
       
        if (incidentCount.next()) {
            categoryCounts[category] = incidentCount.getAggregate('COUNT');
        }
    });


    for (var category in categoryCounts) {
        gs.print("Total Incidents in Category '" + category + "': " + categoryCounts[category]);
    }
})();

dgarad
Giga Sage

Hi @ServiceNow40 

 

Refer the below code .

// count for in active incident
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT');
ga.addInactiveQuery();
ga.query();
while(ga.next()){

	gs.print(ga.getAggregate('COUNT'));
}

// count for active incident
var gr = new GlideAggregate('incident');
gr.addAggregate('COUNT');
gr.addActiveQuery();
gr.query();
while(gr.next()){

	gs.print(gr.getAggregate('COUNT'));
}

 // count of category
var ca = new GlideAggregate('incident');
ca.addAggregate('COUNT','category');
ca.query();
while(ca.next()){

	gs.print(ca.getDisplayValue('category')+'-'+ca.getAggregate('COUNT','category'));
}
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Juhi Poddar
Kilo Patron

Hello @ServiceNow40 

In addition to the solution provided by @Voona Rohila  you can use the following code to print the number of active/inactive incidents for each category:

// Initialize GlideAggregate to group incidents by category and active status
var agg = new GlideAggregate('incident');
agg.addAggregate('count');     // Count incidents
agg.groupBy('category');       // Group by category
agg.groupBy('active');         // Group by active status (true/false)
agg.orderBy('category');       // Order by category
agg.query();

var categoryCounts = {}; // Object to store counts by category

// Process each result in the aggregate
while (agg.next()) {
    var category = agg.category.getDisplayValue() || "Uncategorized"; // Get category name or default
    var isActive = agg.active.toString() == 'true' ? 'Active' : 'Inactive'; // Explicitly check for 'true'
    var count = parseInt(agg.getAggregate('count'), 10); // Parse the count to an integer
    
    // Initialize category object if it doesn't exist
    if (!categoryCounts[category]) {
        categoryCounts[category] = { Active: 0, Inactive: 0 };
    }
    
    // Increment the count for either active or inactive incidents
    categoryCounts[category][isActive] += count;
}

// Output the results
for (var cat in categoryCounts) {
    gs.info(cat + " (Active): " + categoryCounts[cat].Active + " incidents");
    gs.info(cat + " (Inactive): " + categoryCounts[cat].Inactive + " incidents");
}

Result:

JuhiPoddar_0-1731315800928.png

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

Thank You
Juhi Poddar