How do get count of the incident through glide aggregation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 07:03 PM
How do get count of the incident through glide aggregation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 08:05 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 08:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2023 08:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 08:43 PM - edited ‎11-14-2023 08:44 PM
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