Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Script to get count of incident according to priority and display one latest incident for each priority.

Pranav_Thanedar
Giga Sage

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!

4 REPLIES 4

Ankur Bawiskar
Tera Patron

Hi,

have a look on this

Get last updated Date record using GlideAggregate

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

BeingKhan
Kilo Guru

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:

  1. Added groupBy('priority') to ensure proper grouping

  2. Added a nested query to fetch the latest incident for each priority group

  3. Included relevant details from the latest incident (number, creation time, and description)
    Please mark it ✅ helpful if you find it correct.

MD SHADAB KHAN
CSA || CAD || CIS-DISCOVERY
PLEASE MARK THE ANSWER HELPFUL AND ACCEPT IT AS A SOLUTION IF YOU FIND IT HELPFUL & CORRECT

Harish Bainsla
Kilo Patron

Hi @Pranav_Thanedar  check below script

var aggIncident = new GlideAggregate('incident');
aggIncident.addAggregate('MAX', 'sys_created_on');
aggIncident.groupBy('priority');
aggIncident.query();

while (aggIncident.next()) {
var priority = aggIncident.getValue('priority');
var latestTime = aggIncident.getAggregate('MAX', 'sys_created_on');
var latestIncident = new GlideRecord('incident');
latestIncident.addQuery('priority', priority);
latestIncident.addQuery('sys_created_on', latestTime);
latestIncident.query();

if (latestIncident.next()) {
gs.log('Priority: ' + priority +
', Number: ' + latestIncident.getValue('number') +
', Short Description: ' + latestIncident.getValue('short_description') +
', Created On: ' + latestTime);
}
}
Screenshot 2025-02-21 at 11.56.20 AM.png
 if my answer helps you mark helpful and accept solution 

Debasis Pati
Kilo Sage

Hello @Pranav_Thanedar ,

DebasisPati_0-1740122450798.png


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