How do get count of the incident through glide aggregation

njhvgvvvhvhv
Kilo Contributor

How do get count of the incident through glide aggregation

5 REPLIES 5

Kavita_Bhojane
Tera Guru

Hi @njhvgvvvhvhv,

 

You can refer below post:

https://developer.servicenow.com/blog.do?p=/post/training-glideagg/

 

Please mark my answer correct and helpful, if you find it useful.

 

Thanks,

Kavita Bhojane 

abirakundu23
Mega Sage

Hi @njhvgvvvhvhv ,

Please follow below code:

var con = new GlideAggregate('incident');

con.addQuery('active', 'true');

con.addAggregareate('COUNT');

con.query();

var inc =0;

if(con.next()){

var inc = con.getAggregate('COUNT');

gs.log("Incident Count" +inc);

}

Please mark helpful & accepted if it's helpful

 

 

Hi @njhvgvvvhvhv ,

Please follow below code:

var con = new GlideAggregate('incident');

con.addQuery('active', 'true');

con.addAggregareate('COUNT');

con.query();

var inc =0;

if(con.next()){

var inc = con.getAggregate('COUNT');

gs.log ("Incident Count" +inc); // Display incident count with Active true

}

Is above response helpful for you? Do you have any further queries, please post in here.

Please mark helpful & correct answer if it's helpful for you.

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @njhvgvvvhvhv ,

 

In ServiceNow, you can use GlideAggregate to count the number of incidents based on specific conditions. Here's an example of how you can do this:

 

// Initialize GlideAggregate for the 'incident' table

var ga = new GlideAggregate('incident');
// Add conditions if needed using addQuery()
// For example, to count incidents in the 'Resolved' state:
ga.addQuery('state', '3'); // '3' represents the Resolved state
// Add additional conditions if required using addEncodedQuery()
// For example, to count incidents created this month:
ga.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()');
// Add the count aggregation
ga.addAggregate('COUNT');
ga.query();
// Get the count result

var incidentCount = 0;
if (ga.next()) {
    incidentCount = parseInt(ga.getAggregate('COUNT'), 10);
}

// Display the count
gs.info('Number of Incidents: ' + incidentCount);

 

This script demonstrates how to count incidents based on specific conditions using GlideAggregate. Adjust the `addQuery()` and `addEncodedQuery()` criteria based on your requirements, such as filtering by state, assignment group, priority, or any other incident field.

 

The result is retrieved using the `getAggregate()` method, and the count is obtained after parsing it into an integer. Finally, the count is displayed using `gs.info()` for demonstration purposes. You can modify this script to suit your specific use case and handle the count result as needed.

 

Thanks,

Danish