home Homebuild Toolsbug_report Errorsmenu_book Guideslightbulb Tipssmart_toy Promptsextension Extensionsfolder_open Resourcesinfo About
search
error

MALFORMED_QUERY: Aggregate query does not support queryMore()

What does this error mean?

The Salesforce API's queryMore() method paginates through large result sets by calling for the next batch of records using a query locator. However, queryMore() is not supported for aggregate queries — those using GROUP BY, COUNT(), SUM(), and other aggregate functions. Attempting to paginate an aggregate result set triggers this error.

In Apex, this surfaces when using a SOQL for-loop on a large aggregate result set or when the API client attempts to page beyond the first 2,000 aggregate rows.

Common Causes

1. REST API client calling queryMore on aggregate results

An integration executes a GROUP BY query that returns more than 2,000 groups, receives a nextRecordsUrl, and calls that URL — which fails because aggregate queries don't support pagination.

2. Large aggregate query returning 2,000+ groups

A query grouping by a high-cardinality field (e.g., GROUP BY OwnerId in an org with thousands of users) exceeds the 2,000-row aggregate result limit.

How to Fix It

Solution 1: Use LIMIT and OFFSET for pagination

Aggregate queries support LIMIT and OFFSET for manual pagination. Iterate through pages by incrementing the OFFSET.

Apex
Integer batchSize = 200;
Integer offset = 0;
List<AggregateResult> batch;

do {{
    batch = Database.query(
        'SELECT OwnerId, SUM(Amount) total ' +
        'FROM Opportunity GROUP BY OwnerId ' +
        'LIMIT '  + batchSize +
        ' OFFSET ' + offset
    );
    for (AggregateResult ar : batch) {{
        // process each group
    }}
    offset += batchSize;
}} while (batch.size() == batchSize);

Solution 2: Reduce the result set with additional filters

If you're getting thousands of aggregate groups, add WHERE clause filters to limit which records are aggregated — reducing both the query cost and the number of groups returned.

lightbulb

Pro Tip: OFFSET has a maximum of 2,000 in SOQL. If you need more than 2,000 aggregate groups, consider restructuring with multiple targeted queries (e.g., filter by date range per batch) or using Batch Apex to aggregate in memory.