The critical gotcha with GlideAggregate on choice fields like state: you get the raw integer values (1, 2, 3) not the display labels (New, In Progress, On Hold). ServiceNow doesn't automatically resolve choice field labels in aggregate queries because it would require additional joins that kill performance. Most developers expect getValue('state') to return 'New' but get '1' instead, then spend an hour debugging why their results object has numeric keys.
When to use this
- Building dashboard metrics or reporting data where you need counts by category
- Creating SLA reports that group incidents by workflow state
- Scheduled jobs that need to process summary data without loading individual records
- Performance-critical operations where you can't afford to query individual records
When NOT to use this
- Don't use this in client scripts — use
GlideAjaxto call a Script Include that runs this server-side - Don't use this when you need to access other fields from the records — use
GlideRecordwith proper queries instead - Don't use this inside loops over other
GlideRecordresults — you'll create N+1 query performance problems - Don't use this on extended tables without understanding inheritance — child table records may not appear in parent aggregates
Key behaviors and gotchas
- State values are integers: 1=New, 2=In Progress, 3=On Hold, 6=Resolved, 7=Closed, 8=Canceled
- Use
getAggregate('COUNT', 'state')notgetValue('COUNT')— the latter returns undefined - ACLs still apply — your count only includes records the current user can read
- Results include all state values that exist in data, even if no longer in the choice list
- Parse the count with
parseInt()—getAggregate()returns strings - Domain separation affects results — you only see counts from domains you can access
If you have custom state values or modified choice lists, your hardcoded state mapping will be wrong. Query the sys_choice table to build the mapping dynamically, or you'll get 'Unknown' labels for valid states.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.