ServiceNow GlideAggregate cheat sheet GlideAggregate runs COUNT, SUM, MIN, MAX and AVG in the database, so you do not load records just to count them. It extends GlideRecord, so addQuery() and friends work the same way.
All APIs, searchable GlideRecord GlideSystem GlideDateTime Business rules GlideAjax g_form Client scripts, g_user & g_scratchpad Server 4 methods · examples checked with the Script Checker · reviewed October 2026
Counts the records that match the query.
Copy var ga = new GlideAggregate ('incident' );
ga.addActiveQuery ();
ga.addAggregate ('COUNT' );
ga.query ();
var count = ga.next () ? parseInt (ga.getAggregate ('COUNT' ), 10 ) : 0 ; Returns: getAggregate() returns a string, so convert it with parseInt().
Groups the results by a field, giving one row per value.
Copy var ga = new GlideAggregate ('incident' );
ga.addActiveQuery ();
ga.addAggregate ('COUNT' );
ga.groupBy ('priority' );
ga.query ();
while (ga.next ()) {
gs.info (ga.getValue ('priority' ) + ': ' + ga.getAggregate ('COUNT' ));
} addAggregate('SUM' | 'MIN' | 'MAX' | 'AVG', field) # Runs SUM, MIN, MAX or AVG on a numeric field. Here: the average number of reassignments per group.
Copy var ga = new GlideAggregate ('incident' );
ga.addActiveQuery ();
ga.addAggregate ('AVG' , 'reassignment_count' );
ga.groupBy ('assignment_group' );
ga.query ();
while (ga.next ()) {
gs.info (ga.getDisplayValue ('assignment_group' ) + ': ' + ga.getAggregate ('AVG' , 'reassignment_count' ));
} addAggregate('COUNT', field) # Counts per value of a field: a quick way to find duplicates.
Copy var ga = new GlideAggregate ('sys_user' );
ga.addAggregate ('COUNT' , 'email' );
ga.groupBy ('email' );
ga.query ();
while (ga.next ()) {
if (parseInt (ga.getAggregate ('COUNT' , 'email' ), 10 ) > 1 ) {
gs.info ('Duplicate email: ' + ga.getValue ('email' ));
}
} Check your own script Paste a business rule, client script or script include and get every mistake by line, with the fix.
Open the Script Checker Frequently asked questions Why use GlideAggregate instead of getRowCount()? getRowCount() loads every matching record and then counts them. GlideAggregate asks the database for the count, which is much faster on large tables.