The critical difference: getRowCount() executes the full query, loads every matching record into memory, then counts them. GlideAggregate with COUNT generates a SQL COUNT(*) that returns only the number. On large tables, this difference is catastrophic — I've seen getRowCount() operations timeout after loading 250,000+ incident records when all they needed was the count. The platform doesn't optimize this automatically.
When to use GlideAggregate COUNT
- When you only need the count of records, not the record data itself
- On tables with more than 1,000 records in your expected result set
- In scheduled jobs that check record volumes for reporting or thresholds
- When building dashboard widgets that display record counts by category
When NOT to use GlideAggregate COUNT
- When you need to process the actual records after counting — use
GlideRecordwithhasNext()instead - On small result sets (under 100 records) where you'll query the same data again — the performance difference is negligible
- In client-side scripts — neither works there, use
GlideAjaxto call a Script Include - When you need field values from the records —
GlideAggregateonly returns aggregate functions, not field data
Key behaviors and gotchas
GlideAggregate.getAggregate('COUNT')returns a string, not a number — always useparseInt()for math operations- Both methods respect ACLs — users only count records they can read, which can cause confusing discrepancies in reports
getRowCount()has a hard limit of 10,000 records — it will never return more than that, even if more records matchGlideAggregaterespects database indexes — queries on indexed fields likestate,priority, andassigned_toperform dramatically better- Empty result sets require explicit handling —
GlideAggregate.next()returnsfalsewhen no records match - Domain separation affects both — counts only include records in accessible domains, not global counts
Never use getRowCount() in a loop or scheduled job on large tables. I've seen this pattern bring down entire instances when it hits tables like sys_journal_field or sys_audit with millions of records.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.