Skip to content

The Soql.AggregateResult Class

Jason Siders edited this page Jul 4, 2025 · 5 revisions

This class decorates a Schema.AggregateResult, which cannot be mocked otherwise. Objects of this type are returned by the Soql class's aggregateQuery method, and can be mocked via the MockSoql.AggregateResult class.

Soql.Aggregation count = new Soql.Aggregation(Soql.Function.COUNT, User.Id);
List<Soql.AggregateResult> results = DatabaseLayer.Soql.newQuery(User.SObjectType)
  ?.addSelect(count)
  ?.toSoql()
  ?.aggregateQuery();
Soql.AggregateResult firstResult = results?.get(0);
Integer numUsers = firstResult?.get('expr0');

Methods

get

Calls the underlying Schema.AggregateResult object's get method. The key parameter refers to the field alias if one is assigned, or the parameter's index in query preceded by the expr if one is not assigned (x, expr0).

  • Object get(String key)

apex-database-layer

Home

Core Concepts

Reference Guide

Migration Gudes

Clone this wiki locally