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.

Server4 methods · examples checked with the Script Checker · reviewed October 2026

addAggregate('COUNT')

#

Counts the records that match the query.

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().

groupBy(field)

#

Groups the results by a field, giving one row per value.

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.

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.

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'));
    }
}

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.