You need to sign in to do that
Don't have an account?
Flauradel Consumido
Roll up summary of Opportunity Amount attached on on Custom Object
Hi All, I would like to ask for any help as I am really new to this and still learning. I am trying to create a trigger that will roll up the Opportunity Amount on a Custom Object field. Their relationship is lookup only. 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__c
Field: Total_Amount__c
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__c
Field: Total_Amount__c
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; }
I suppose that the trigger should be on Opportunity, because whenever a new Opportunity with a amount is created for a Commission__c, the Total_Amount__c on it should be updated.
Please try below trigger as per the requirement.
Kindly let me know if that helps you.
Best Regrads,
BALAJI
All Answers
Hi there,
You should be able to accomplish this without a trigger. Create a custom field on the Custom Object with a data type of "Roll Up Summary". To configure the field, select the following (see screenshot below):
Hope this helps!
Brian
I suppose that the trigger should be on Opportunity, because whenever a new Opportunity with a amount is created for a Commission__c, the Total_Amount__c on it should be updated.
Please try below trigger as per the requirement.
Kindly let me know if that helps you.
Best Regrads,
BALAJI
Super thank you for the trigger. However, can I modify this to also roll up not only upon creating the record by upon update as well?