Salesforce supoorts Roll Up summary fields only with Master-Detail relationship. Yet if you need to have it without Master-Detail, you will have to implement its functionality by your own. For instance you may use Apex trigger on child object to compute custom roll up summary value on parent record.
Ok, you me refer below code. This code updates custom roll up summary field "Roll_Up_Summary_Count__c" on parent object, by the number of children records. Similarly you can implement your solution for MAX, AVG and MIN etc.
Trigger SomeTriggerName On Child_Object__c (After Insert){
Set<Id> setParentIds=new Set<Id>();
for(Child_Object__c c:Trigger.new)
setParentIds.add(c.parent_relation_field__c);
List<Parent_Object__c> lstParentsToUpdate=new List<Parent_Object__c>();
for(AggregateResult result:[Select parent_relation_field__c,count(id) From Child_Object__c WHERE parent_relation_field__c IN :setParentIds GROUP BY parent_relation_field__c LIMIT 2000]){
Parent_Object__c parent=new Parent_Object__c();
parent.Id=result.get('parent_relation_field__c');
parent.Roll_Up_Summary_Count__c=(Integer)result.get('expr0');
lstParentsToUpdate.add(parent);
}
update lstParentsToUpdate;
}
I came accross your code and was trying this, however, I keep receiving Error: Compile Error: Invalid field: 'Commission__c' at line 6 column 32
I have a Custom object and set up a lookup from Opportunity to the custom object. What I would like is to get the sum of the Amount of all the opportunities attached to the custom object.
I need to roll up: From Object: Opportunity Field: Amount
Where to roll up: Object: Commission Field: Total_Amount__c
Below is the code:
trigger CommissionRollUp on Commission__c (After Insert){
Set<Id> setOpportunityIds=new Set<Id>();
for(Commission__c c:Trigger.new)
setOpportunityIds.add(c.Amount);
List<Opportunity> lstOpportunityToUpdate=new List<Opportunity>();
for(AggregateResult result:[Select Commission__c,count(id) From Commission__c WHERE Commission__c IN :setOpportunityIds GROUP BY Commission__c LIMIT 2000]){
Opportunity__r parent=new Opportunity__r();
Opportunity.Id=result.get('Commission__c');
Opportunity.Total_Amount__c=(Integer)result.get('expr0');
lstOpportunityToUpdate.add(Opportunity);
}
update lstOpportunityToUpdate;
}
The solution needs to have an Apex trigger on child object which, in your case, is Opportunity not Commission__c.
The trigger should run the logic for every possible event that may affect your roll-up value, for instance insert, delete and may be update event too. Below code only handles "insert" event, you may need to add code for other required events.
trigger CommissionRollUp on Opportunity (After Insert){
Set<Id> setCommissionIds = new Set<Id>();
for(Opportunity opp : Trigger.new)
setCommissionIds.add(opp.Commission__c);
List<Commission__c> lstCommissionsToBeUpdated = new List<Commission__c>();
for(AggregateResult result:[ Select Commission__c,
SUM(Amount)
From Opportunity
WHERE Commission__c IN :setCommissionIds
GROUP BY Commission__c LIMIT 2000]){
Commission__c com = new Commission__c();
com.Id = result.get('Commission__c');
com.Total_Amount__c = (Integer)result.get('expr0');
lstCommissionsToBeUpdated.add(com);
}
update lstCommissionsToBeUpdated;
}
Salesforce supoorts Roll Up summary fields only with Master-Detail relationship. Yet if you need to have it without Master-Detail, you will have to implement its functionality by your own. For instance you may use Apex trigger on child object to compute custom roll up summary value on parent record.
Can you plz share a sample of that code for reference.
Thanks,
I have a Custom object and set up a lookup from Opportunity to the custom object. What I would like is to get the sum of the Amount of all the opportunities attached to the custom object.
I need to roll up:
From Object: Opportunity
Field: Amount
Where to roll up:
Object: Commission
Field: Total_Amount__c
Below is the code:
Hope this helps.