When an Opportunity needs to have an amount rolled up from a custom object that is not in a master-detail relationship, the best approach is to:
Option B: Write a trigger on the child object and use an aggregate function to sum the amount for all related child objects under the Opportunity.
Roll-Up Summary Fields Limitation: Roll-up summary fields can only be created on master-detail relationships. Since the custom object is in a lookup relationship with Opportunity (not master-detail), roll-up summary fields are not available.
"Roll-up summary fields can only be created on master-detail relationships."— Salesforce Help: Define Roll-Up Summary Fields
Trigger on Child Object: Writing a trigger on the child object (the custom object) allows you to execute logic whenever a child record is inserted, updated, or deleted.
Aggregate Function: Within the trigger, you can use SOQL aggregate functions (like SUM()) to calculate the total amount of all child records related to the Opportunity and update the parent Opportunity record accordingly.
trigger ChildObjectTrigger on ChildObject__c (after insert, after update, after delete, after undelete) {
Set oppIds = new Set();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(ChildObject__c child : Trigger.new){
oppIds.add(child.Opportunity__c);
}
}
if(Trigger.isDelete){
for(ChildObject__c child : Trigger.old){
oppIds.add(child.Opportunity__c);
}
}
List oppsToUpdate = new List();
for(Id oppId : oppIds){
Decimal totalAmount = [SELECT SUM(Amount__c) FROM ChildObject__c WHERE Opportunity__c = :oppId];
Opportunity opp = new Opportunity(Id = oppId, Total_Child_Amount__c = totalAmount);
oppsToUpdate.add(opp);
}
update oppsToUpdate;
}
Why Other Options Are Incorrect:
Option A: Use the Streaming API to create real-time roll-up summaries.
The Streaming API is designed for receiving notifications of data changes in Salesforce and is not suitable for calculating roll-up summaries.
"The Streaming API enables streaming of events using push technology and provides a subscription mechanism for receiving events in near real time."— Salesforce Developer Guide: Streaming API
Option C: Use the Metadata API to create real-time roll-up summaries.
The Metadata API is used for deploying and retrieving metadata (like custom fields, layouts) between orgs and does not perform data calculations.
"Use Metadata API to retrieve, deploy, create, update, or delete customization information, such as custom object definitions and page layouts, for your org."— Salesforce Developer Guide: Metadata API
Option D: Write a trigger on the Opportunity object and use tree sorting to sum the amount for all related child objects under the Opportunity.
Writing a trigger on the Opportunity object would not effectively capture changes made to child records, as triggers on the parent do not fire when child records are modified.
Tree sorting is not a standard technique in Apex for summing related records.
Conclusion:
Writing a trigger on the child object is the most effective way to sum amounts from child records and update the parent Opportunity when a master-detail relationship is not present.