function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Flauradel ConsumidoFlauradel 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
 
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;
}

 
Best Answer chosen by Flauradel Consumido
BALAJI CHBALAJI CH
Hi Flauradel Consumido,

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.
 
trigger CommissionRollUp1 on Opportunity (After Insert){
    Set<Id> setOpportunityIds=new Set<Id>();
    for(Opportunity c:Trigger.new)
        setOpportunityIds.add(c.id);
    
    List<Commission__c> ListOfAllOrgs = [select id, Total_Amount__c from Commission__c];
    List<Commission__c> lstCommissionToUpdate=new List<Commission__c>();
    
    for(AggregateResult result:[Select Commission__c, SUM(Amount) From Opportunity 
                                WHERE Id IN :setOpportunityIds GROUP BY Commission__c])
    {
        for(Commission__c org : ListOfAllOrgs)
        {
            if(result.get('Commission__c') == org.Id)
            {
                if(org.Total_Amount__c == null)
                {
                    org.Total_Amount__c = 0;
                }
                org.Total_Amount__c = org.Total_Amount__c + Decimal.ValueOf(String.ValueOf(result.get('expr0')));
                lstCommissionToUpdate.add(org);
            }
        }
    }
    update lstCommissionToUpdate;
}

Kindly let me know if that helps you.

Best Regrads,
BALAJI

All Answers

Brian SheaBrian Shea

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):

  • Summarized Object = "Opportunity"
  • Rollup Type = "Sum"
  • Field to Aggregate = "Amount"
Roll up Summary configuration

Hope this helps!


Brian

Flauradel ConsumidoFlauradel Consumido
Thanks Brian. However, roll-up summary only works for Master-Detail relationship. The custom object and Opportunity only has Lookup relationship. Opportunity cannot be a child object of a Custom Object since its a SObject.
BALAJI CHBALAJI CH
Hi Flauradel Consumido,

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.
 
trigger CommissionRollUp1 on Opportunity (After Insert){
    Set<Id> setOpportunityIds=new Set<Id>();
    for(Opportunity c:Trigger.new)
        setOpportunityIds.add(c.id);
    
    List<Commission__c> ListOfAllOrgs = [select id, Total_Amount__c from Commission__c];
    List<Commission__c> lstCommissionToUpdate=new List<Commission__c>();
    
    for(AggregateResult result:[Select Commission__c, SUM(Amount) From Opportunity 
                                WHERE Id IN :setOpportunityIds GROUP BY Commission__c])
    {
        for(Commission__c org : ListOfAllOrgs)
        {
            if(result.get('Commission__c') == org.Id)
            {
                if(org.Total_Amount__c == null)
                {
                    org.Total_Amount__c = 0;
                }
                org.Total_Amount__c = org.Total_Amount__c + Decimal.ValueOf(String.ValueOf(result.get('expr0')));
                lstCommissionToUpdate.add(org);
            }
        }
    }
    update lstCommissionToUpdate;
}

Kindly let me know if that helps you.

Best Regrads,
BALAJI
This was selected as the best answer
Flauradel ConsumidoFlauradel Consumido
Hi BALAJI CH,

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?
Flauradel ConsumidoFlauradel Consumido
So sorry for the late response as I was crazy busy. 
Flauradel ConsumidoFlauradel Consumido
I tried it again and it is working. I think I was not doing the test properly before. Thank you!!! :D
Lindsay SmithLindsay Smith
I tried to create a trigger as described above for the same issue.  My custom object is called Feature Request. Can you please help me with the error below?

User-added image