Script to get count of incident according to priority and display one latest incident for each priority.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2022 08:00 AM
I need a script for below requirement.
The script should output the count of incidents for each priority. I was able to achieve this using the below script.
var gra = new GlideAggregate('incident');
gra.addAggregate('COUNT', 'priority');
gra.query();
while (gra.next()) {
gs.print(gra.getAggregate('COUNT', 'priority'));
}
The further requirement is that the script should display only one latest incident for each priority. All the above should be done in a single script. Could you please help? Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2022 08:05 AM
Hi,
have a look on this
Get last updated Date record using GlideAggregate
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 09:26 PM
Here's a script that combines both counting incidents by priority and displaying the latest incident for each priority:
var gra = new GlideAggregate('incident'); gra.addAggregate('COUNT', 'priority'); gra.groupBy('priority'); // Group by priority to get counts per group gra.query(); while (gra.next()) { // Get priority display value and count var priorityName = gra.priority.getDisplayValue(); var incidentCount = gra.getAggregate('COUNT', 'priority'); // Output count information gs.print("Priority: " + priorityName + " | Total Incidents: " + incidentCount); // Get latest incident for this priority var latestIncident = new GlideRecord('incident'); latestIncident.addQuery('priority', gra.getValue('priority')); // Query by sys_id latestIncident.orderByDesc('sys_created_on'); latestIncident.setLimit(1); latestIncident.query(); if (latestIncident.next()) { gs.print("-> Latest Incident:"); gs.print(" Number: " + latestIncident.getValue('number')); gs.print(" Created: " + latestIncident.getValue('sys_created_on')); gs.print(" Short Description: " + latestIncident.getValue('short_description')); } gs.print("--------------------------------------------------"); }
Key modifications:
Added groupBy('priority') to ensure proper grouping
Added a nested query to fetch the latest incident for each priority group
Included relevant details from the latest incident (number, creation time, and description)
Please mark it ✅ helpful if you find it correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 10:27 PM
Hi @Pranav_Thanedar check below script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 11:21 PM
Hello @Pranav_Thanedar ,
Please try the below script:
// Create a GlideAggregate object to count incidents by priority
var incidentCount = new GlideAggregate('incident');
// Group by priority and count the incidents
incidentCount.addAggregate('COUNT', 'priority');
// Query the incident records grouped by priority
incidentCount.groupBy('priority');
incidentCount.query();
// Iterate through each priority
while (incidentCount.next()) {
var priority = incidentCount.priority + ''; // Convert GlideRecord to string
var count = incidentCount.getAggregate('COUNT', 'priority');
// Query the latest incident for each priority
var latestIncident = new GlideRecord('incident');
latestIncident.addQuery('priority', priority);
latestIncident.orderByDesc('sys_created_on'); // Sort by the creation date in descending order
latestIncident.setLimit(1); // Limit to only 1 result (latest incident)
latestIncident.query();
if (latestIncident.next()) {
// Print the count of incidents and details of the latest incident for each priority
gs.info('Priority: ' + priority + ' - Count: ' + count + ' - Latest Incident: ' + latestIncident.number + ' - Created On: ' + latestIncident.sys_created_on);
}
}
Please mark it correct if this helps you.
Regards,
Debasis