What It Does

The setGroup() method controls whether a GlideAggregate query groups results by specified fields or calculates aggregates across all matching records without grouping. When isGrouped is true, the query honors all groupBy() calls and returns one result row per unique combination of grouped fields.

When isGrouped is set to false, ServiceNow ignores all previously defined groupBy() calls and executes the aggregate functions across the entire result set. This produces a single row containing the aggregate values calculated from all records matching the query conditions. The SQL generated changes from a GROUP BY query to a simple aggregate SELECT without grouping clauses.

The method returns void and must be called before query() to affect the query execution. Calling setGroup() after query() has no effect on the already-executed query results. The grouping behavior is determined at query execution time and cannot be changed retroactively.

When grouping is disabled, field values from groupBy() calls become inaccessible in the result set since they're not part of the aggregate calculation. Attempting to access these fields returns empty values. Only aggregate function results (addAggregate()) remain valid when grouping is disabled.

The method works in conjunction with groupBy() and addAggregate() to provide flexible aggregate query patterns. Unlike orderBy() or addQuery(), this method fundamentally changes the query structure rather than just adding conditions or sorting parameters.

When to Use This

Use setGroup(false) when you need overall totals across all records rather than grouped calculations. This is essential for dashboard widgets showing grand totals, reports requiring summary statistics, or any scenario where you want a single aggregate value from multiple records. The method is particularly valuable when building reusable functions that can switch between grouped and ungrouped aggregates based on runtime parameters.

Avoid using setGroup(false) when you actually need to see individual group values or when the grouping fields contain important data for your logic. Instead, use standard grouped aggregates and iterate through results to calculate totals in script if needed. Don't use this method as a performance optimization—ungrouped aggregates aren't necessarily faster than grouped ones, especially on large datasets.

⚠️

Common mistake: calling setGroup(false) and then trying to access groupBy() field values. These fields return empty when grouping is disabled.

Return Value

The method returns void and cannot fail. It's a configuration method that sets internal state on the GlideAggregate object. No validation occurs on the isGrouped parameter—non-boolean values are converted using JavaScript's standard truthy/falsy evaluation.

Since there's no return value, you cannot chain this method call or use it in conditional expressions. The effect of the method is only visible after calling query() and examining the result set structure through next() and aggregate value retrieval methods.

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 since this only modifies query structure, not data
  • Changes the generated SQL from GROUP BY queries to simple aggregate SELECT statements
  • Performance depends on table size and indexes—ungrouped aggregates can be faster or slower than grouped ones
  • Must be called before query() execution—has no effect if called after query() completes
  • No database writes occur—this is a read-only query modification
  • Same security context applies as other GlideAggregate methods—respects table ACLs and field-level security
  • Works identically in Business Rules, Script Includes, and Scheduled Jobs—no scope-dependent behavior