The critical insight with GlideAggregate is that getAggregate() returns string values, not numbers. This trips up every developer who assumes database aggregation functions return numeric types. You must explicitly parse the result to float or you'll get string concatenation instead of mathematical addition in downstream calculations. Additionally, null aggregate results become empty strings, not zero, which breaks numeric operations unless you provide a fallback.
When to use this
- When calculating financial totals across purchase orders, invoices, or budget allocations
- When you need grouped sums by vendor, department, or category in a single query
- When summing effort hours, costs, or quantities from incident, change, or project tables
- When the result set is large enough that loading individual records would be inefficient
When NOT to use this
- Don't use this in client scripts β create a Script Include with
client_callable: trueand call via GlideAjax - Don't use this when you need other field values from the same records β use GlideRecord instead
- Don't use this on currency fields with different base currencies β the sum will be meaningless
- Don't use this in a loop over another query β you'll create N+1 database queries
Key behaviors and gotchas
getAggregate()always returns a string β useparseFloat()with a fallback to zero- Null values in the aggregated field are excluded from SUM β they don't become zero
groupBy()works on sys_id values for reference fields, not display values- ACLs apply to GlideAggregate β users only see totals from records they can read
- Domain separation affects results β cross-domain totals require elevated privileges
- Empty result sets still call
next()once but return empty string fromgetAggregate()
Currency fields with mixed base currencies will sum the raw numeric values, ignoring exchange rates. Convert to a common currency first or filter by currency before aggregating.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.