What It Does

The getAggregate() method retrieves the calculated result of a specific aggregate function that was previously added to a GlideAggregate query using addAggregate(). The method acts as an accessor to the aggregated data after the query has been executed and positioned on a specific group using next().

Internally, ServiceNow executes the aggregate query against the database and stores the results in memory. When you call getAggregate(), the platform looks up the specific aggregate function and field combination from the current row's calculated values. The database engine performs the actual aggregation—COUNT, SUM, AVG, MIN, or MAX—and returns the result to the GlideAggregate object.

The method always returns a string, regardless of the underlying data type of the aggregated field. Numeric results from SUM or AVG operations are converted to strings, dates become date strings, and COUNT operations return string representations of integers. If the aggregate function or field name doesn't match what was defined with addAggregate(), the method returns an empty string rather than null or throwing an error.

Edge cases include calling getAggregate() before calling next(), which returns an empty string since no row is positioned. When aggregating fields that contain null values, COUNT ignores nulls while SUM treats them as zero. AVG calculations exclude null values from both numerator and denominator, potentially causing confusion when comparing against manual calculations.

This method works in conjunction with addAggregate() to define the aggregates, groupBy() to define grouping fields, and query() to execute the aggregation. Unlike getValue() which retrieves grouped field values, getAggregate() specifically retrieves calculated aggregate results.

When to Use This

Use getAggregate() when you need to retrieve the results of aggregate calculations after positioning on a specific group with next(). This is essential for building reports, dashboards, or any functionality that requires summarized data across groups of records. Common scenarios include counting incidents by state, summing purchase order amounts by vendor, or finding average resolution times by assignment group.

Don't use getAggregate() when you only need a single aggregate value without grouping—use getRowCount() for simple record counts or create a GlideAggregate without groupBy() for single aggregate values. Avoid this method when you need the actual field values from grouped records—use getValue() instead. The most common misuse is attempting to call getAggregate() for aggregates that weren't defined with addAggregate(), which silently returns empty strings and creates hard-to-debug issues.

Return Value

Returns a string representation of the aggregate calculation result. Numeric values are converted to strings with standard JavaScript number-to-string conversion rules—integers appear without decimal points, floating-point numbers include decimals as needed. Date and datetime fields aggregated with MIN or MAX return ISO-formatted date strings. COUNT operations always return whole number strings like "0", "5", or "1247".

When the specified aggregate function and field combination doesn't exist, or when called before next() positions on a valid row, the method returns an empty string (""). Never returns null or undefined, making it safe to use in string concatenation but requiring explicit empty string checks for conditional logic. Convert to numbers using parseInt() or parseFloat() when performing mathematical operations on the results.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No Business Rules, ACLs, or notifications fire—this is a read-only data access method that doesn't trigger platform automation
  • Nothing is written to the database—the method only retrieves pre-calculated aggregate results from the query execution
  • Performance is fast after initial query execution since results are cached in memory for the current GlideAggregate object instance
  • Works identically across Business Rules, Script Includes, and Scheduled Jobs with no behavioral differences based on execution context
  • Respects the same security model as the underlying table—users can only see aggregate results for records they have read access to
  • Results reflect the database state at query execution time—subsequent record changes don't affect the aggregate values until a new query is executed
  • No audit trail entries are created since this is purely a data retrieval operation with no record modifications