Incident Management
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 11:58 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 12:14 AM
Refer below link, which has the code for your requirement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 12:59 AM
Hi ,
You can use below script for total numbers of active/inactive incidents in Background script
script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 01:02 AM
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'));
}
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 01:03 AM
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:
"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
