In this blog post, we will explore an example that shows how to roll up child counts using maps collection. It will also demonstrate how to sum values on a parent record. This example applies to lookup relationships but you can use the same with master-detail also.
Example Scenario:
Imagine a company uses Salesforce to track projects and their associated deliverables. Each deliverable is a child record (B__c), and projects are parent records (A__c). Each project includes fields for tracking the number of deliverables (Record_Count__c) and the cumulative value of those deliverables (Total__c).
The system requires real-time updates to these project fields whenever deliverables are added, updated, deleted, or moved between projects. For example:
- Adding Deliverables: When a deliverable is added to a project, the project’s deliverable count and total value should increase.
- Updating Deliverables: If the deliverable’s value changes, the original and new projects must reflect the changes. Similarly, if it is reassigned to a different project, this change must also be updated in both projects.
- Deleting Deliverables: When a deliverable is deleted, the project’s deliverable count and total value must decrease accordingly.
- Restoring Deliverables: If a deleted deliverable is restored, the related project’s fields must be updated to include it again.
This automation ensures that project managers have accurate, up-to-date information without manual effort. The RollUpBOnA trigger manages these updates seamlessly.
Trigger Overview:
This Salesforce Apex trigger keeps parent project records (A__c) up-to-date by rolling up information from child deliverables (B__c).
What it Updates :
- Record_Count__c: Number of child records associated with the parent.
- Total__c: Sum of the
Value__cfield from all child records.
When it Runs:
The trigger fires when child records (B__c) are:
- ✨ Created (Insert)
- 🔄 Updated
- ❌ Deleted
- ↩️ Undeleted
How It Works:
Here is a step-by-step explanation of what the RollUpBOnA trigger does:
- Declare a Set for Parent Record IDs:
The trigger begins by declaring a set. It usesparentIdsto store the IDs of the parentA__crecords. These records need to be updated. - Identify Relevant Parent IDs for Insert or Undelete Events:
- During
insertorundeleteevents, the trigger loops through theTrigger.newcollection. - If a child record (
B__c) has a non-null parent (A_No__c), the parent ID is added to theparentIdsset.
- During
- Identify Relevant Parent IDs for Delete Events:
- During
deleteevents, the trigger loops through theTrigger.oldcollection. - If a deleted child record has a non-null parent, the parent ID is added to the
parentIdsset.
- During
- Handle Updates to Parent IDs or Values:
- During
updateevents, the trigger compares the old (Trigger.oldMap) and new (Trigger.new) versions of eachB__crecord. - If the parent ID (
A_No__c) changes, both the old and new parent IDs are added to the set. - If the parent ID remains the same but the value (
Value__c) field changes, add the parent ID to the set.
- During
- Exit if No Parent IDs to Update:
If no parent IDs are collected in the set, the trigger exits. There is no further processing. - Aggregate Data:
Two maps are used:childRecordCount: Tracks the number of deliverables for each parent.childValuesSum: Tracks the totalValue__cfor each parent.- An efficient SOQL query retrieves child record data and populates these maps.
- Retrieve and Prepare Parent Records for Update:
- The trigger queries all
A__crecords in theparentIdsset, retrieving theirRecord_Count__candTotal__cfields. - It updates these fields based on the values stored in the maps (
childRecordCountandchildValuesSum). If a parent ID is not found in the maps, the corresponding field is set to 0.
- The trigger queries all
- Collect Parent Records to Update:
Each updated parent record is added to a list (parentsToUpdate) for processing. - Perform the Update Operation:
Finally, the trigger updates all theA__crecords in theparentsToUpdatelist. This ensures theirRecord_Count__candTotal__cfields are accurate. They are also kept up-to-date.
Sample Code-
Key Benefits:
- 🚀 Efficiency: Uses maps to efficiently calculate roll-up data, minimizing database interactions and avoiding unnecessary processing.
- 🎯 Accuracy: Ensures parent records reflect real-time changes in child records.
- 💪 Robustness: Handles null values and edge cases gracefully.
- 🔄 Automation: Eliminates manual updates to roll-up fields.
Note – The above code shows an example of using map for calculations. If you want to use the aggregate query approach, please refer to this blog post Roll up summary trigger To Update Child Records Count and total Sum of values of child records on Parent Record using aggregate query.
For more helpful articles please visit – https://thesalesforcedev.in


Leave a reply to Comparing Aggregate Query vs Map in Salesforce Apex – Welcome to The Salesforce Dev Cancel reply