GlideRecord fetches actual rows from a ServiceNow table, while GlideAggregate fetches calculations such as counts, sums and averages done on the server. Both are server-side scripting objects, and you pick one based on whether you need the records themselves or just a number about them. If you only need to know how many, or a total, GlideAggregate is the right tool and far lighter on the instance.
What GlideRecord Does
GlideRecord works row by row. You create one against a table, add your conditions, run the query, then step through the results with next(). Each time you call next(), one full record is loaded, and you can read its fields with getValue() or dot-walking. Use it when you need field values: to copy a record, update a field, check a state, or display information from each row.
A typical flow looks like this: create the GlideRecord, call addQuery() to narrow the set, call query() to run it, then loop with while (gr.next()) and do your work inside the loop. If you plan to change records, call update() inside the loop; if you only read, avoid update() entirely. Set a limit with setLimit() when you can, so a broad query cannot drag in a huge number of rows.
What GlideAggregate Does
GlideAggregate asks the database to do the maths and hand back only the result. You still addQuery() to filter, but you also call addAggregate() with an operation such as COUNT, SUM, AVG, MIN or MAX, and optionally groupBy() to break the answer into buckets. The database returns one small result set instead of every matching row. That is the whole point: the work happens where the data lives, and your script never sees the individual records.
Reading the answer takes two calls. After query() and next(), getAggregate(‘COUNT’) (or whichever operation you chose) returns the value as a string, so wrap it in parseInt() or a similar conversion if you want a number. When you group, each pass through next() gives you one group, and getValue() on the grouped field tells you which bucket you are in.
A Worked Example: Open Incidents by Priority
Say you want a breakdown of open incidents by priority for a dashboard widget or a report email. With GlideAggregate you would query the incident table, add a condition of active equals true, addAggregate(‘COUNT’), groupBy(‘priority’), then loop. Each loop pass reads the priority value and its count, and you build a small object or a formatted string from those two values. The instance processes the whole table once and returns one row per priority.
Now picture the same job with GlideRecord. You would have to query every open incident, walk each one, and keep a running tally in a script object. The answer is identical, but the instance has to hand your script every single record to get there. On a small table you will not notice. On a busy incident table the difference is the difference between a quick query and a slow one.
Where Scripts Go Wrong
Counting with GlideRecord
The most common mistake is using GlideRecord with getRowCount() for counting. It works, but it forces the instance to retrieve every matching row just to throw most of them away. If the answer is a number and you do not need the rows, switch to GlideAggregate with COUNT. Reserve getRowCount() for cases where you are already looping the records for another reason.
Forgetting the String Conversion
getAggregate() hands back a string, not a number. Scripts that compare it to a number or do arithmetic on it can behave oddly or fail quietly. Convert the value as soon as you read it, and your comparisons will behave the way you expect.
Skipping query() or next()
Both objects need query() before anything is returned, and GlideAggregate needs at least one next() before getAggregate() has a value to give you. Leaving either call out returns an empty or undefined result, and the bug looks like a data problem rather than a script order problem.
Running These on the Client
Both objects are server-side. In a client script, the usual substitute is GlideAjax or the REST-style APIs, which call a script include that does the server work and returns only the result. Trying to run GlideAggregate in the browser simply fails, and the fix is to move the logic into a script include and call it from the client.
Choosing Between Them
A simple rule covers most cases. If your script reads or changes fields on individual records, use GlideRecord. If your script only needs a count, total, average, minimum or maximum, use GlideAggregate. When you need both, such as a total plus the top few records, run the aggregate for the number and a limited GlideRecord for the rows, rather than one big query doing double duty. Field names and available operations are documented in ServiceNow’s developer reference for each release, so check there for the exact spelling of the operation you need, since labels and support can differ by version.