Introduction
In Salesforce, roll-up triggers are a common technique to summarize child records’ data into parent records. Whether you’re counting related records or adding up specific field values, your approach choice greatly affects the solution’s efficiency. It also impacts maintainability. Two popular strategies are Aggregate Query and Map-Based Manual Aggregation.
You can refer our previous blogs to see both the approaches in detail –
- Roll up summary trigger To Update Child Records Count and total Sum of values of child records on Parent Record using aggregate query
- Roll up summary trigger To Update Child Records Count and total Sum of values of child records on Parent Record using Maps
While both approaches aim to achieve the same outcome, they differ in implementation, performance, and scalability. This blog explores the pros and cons of each approach. It helps you choose the one that best fits your requirements.
Let’s see the benefits and differences between both of these approaches –
Aggregate Query Approach
Mechanism:
- Utilizes SOQL aggregate functions (
COUNT,SUM, etc.) withGROUP BYclauses to compute rolled-up values directly in the query. - The results are stored in
AggregateResultobjects for further processing. - Sample Code
Pros:
- Efficiency in Querying:
- Offloads computational work to the Salesforce database, making queries efficient and reducing Apex processing overhead.
- Simpler Code:
- The logic is compact since aggregation is handled within the SOQL query.
- Less Memory Usage:
- Requires fewer objects in memory as results are already grouped and computed.
- Dynamic Field Adjustments:
- Easy to adjust the query for new fields or aggregate functions.
Cons:
- Pagination Challenges
- Lack of Pagination Support:
Aggregate queries do not natively support pagination. LIMIT and OFFSET can only retrieve up to 4,000 records, making it impractical for very large datasets.
- Lack of Pagination Support:
- Subquery and Relationship Limitations
- Aggregate functions cannot be applied to subqueries, restricting hierarchical data processing.
- Grouping certain relationships can result in random errors with no clear resolution.
- Field and SOQL Constraints
- Formula Field Aggregation:
Fields such as formulas cannot be included in aggregate computations. - Limited Aggregate Functions:
Only basic operations likeSUM(),MIN(),MAX(),AVG()andCOUNT()are available, restricting advanced computations. - GROUP BY Parent Record Cap:
The GROUP BY operation can handle only up to 200 unique parent records per query. Exceeding this limit results in runtime exceptions. - Sorting and Ordering Constraints:
Aggregate queries do not allow sorting grouped results usingORDER BY. - SOQL Character Limit Constraints:
Queries must stay under 100k characters, and strings in theWHEREclause cannot exceed 4,000 characters.
- Formula Field Aggregation:
- Complexity with Large Data Sets:
- Row Limits:
Queries involving over 50,000 child records risk hitting Salesforce governor limits. - QueryMore Incompatibility:
SOQL For Loop Queries including an aggregate function don’t support queryMore. A run-time exception occurs if you use a query containing an aggregate function. This happens when the aggregate function returns more than 2,000 rows in a for loop.
- Row Limits:
- Restrictions on Batch Jobs
- Aggregate queries cannot be used as the source query for batch jobs. This limitation reduces their applicability in scheduled operations.
Map-Based Manual Aggregation
Mechanism:
- Queries child records without aggregation.
- Uses nested
Mapobjects in Apex to compute the roll-up values programmatically. - Sample Code
Pros:
- Control and Customization:
- Provides full control over how data is aggregated, allowing complex roll-up logic not supported by SOQL (e.g., conditional or weighted sums).
- No Grouping Limits:
- Avoids the
GROUP BYlimit of 200 parent records, making it suitable for larger data sets.
- Avoids the
- Flexibility:
- Supports iterative improvements to aggregation logic without altering the query structure.
Cons:
- Performance Overhead:
- Aggregation logic runs in Apex, which can be slower and more resource-intensive than database-level processing.
- Higher Memory Usage:
- Requires storing and iterating over individual child records in memory, which could lead to heap size issues for large datasets.
- Complexity in Code:
- Code becomes more verbose and harder to maintain compared to aggregate queries.
When to Use Each Approach
| Scenario | Use Aggregate Query | Use Map-Based Aggregation |
|---|---|---|
| Data Volume | Small to moderate data sets (<200 parents). | Large data sets (>200 parents or many children). |
| Logic Complexity | Simple roll-ups (e.g., COUNT, SUM). | Complex computations or custom logic. |
| Performance Requirements | Prioritize database efficiency. | Prioritize flexibility over performance. |
| Maintainability | Cleaner and concise code. | Advanced and evolving requirements. |
Key Takeaways
- Aggregate Query is preferred for straightforward roll-ups and when data volume is within limits.
- Map-Based Aggregation is better for handling large datasets, complex logic, or when avoiding SOQL limitations.
- The choice depends on the use case’s scale, performance requirements, and customization needs.


Leave a comment