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 for Opportunity

I have a custom object "Commission", and Opportunity has a lookup to the custom object. Each "Commission" could have a handful of Opportunities related to it. Need a basic trigger that grabs the total of the Opportunity Amounts attached to the commission and updates a currency field on the Commission record record. 
Kapil KaushikKapil Kaushik
Hi Flauradel,

Have you tried to do this ? Do you need complete code or If you have some code then please post your code here to get early and quick responses.

Thanks,
Kapil Kaushik
buggs sfdcbuggs sfdc
HI

Try the below code
trigger OpportunityLineItemTrigger on OpportunityLineItem (after insert,after update,after delete) {

    List<OpportunityLineItem> lineItems;
    
  if (Trigger.isDelete) 
        lineItems = Trigger.old;
    else
        lineItems = Trigger.new;
    
        if (lineItems != null && lineItems.isEmpty() == false) {
           
            //Opportunity Ids
            Set<Id> opportunityIds = new Set<Id>();

            for (OpportunityLineItem lineItem : lineItems) {
                opportunityIds.add(lineItem.OpportunityId);
            }

            //Map of Opportunities
            Map<Id, Opportunity> opportunities = new Map<Id, Opportunity>([SELECT Id, Rollup_Amount__c FROM Opportunity WHERE Id IN :opportunityIds]);

            //Rollup_Amount__c
            Map<Id, double> oppAmounts = new Map<Id, double>();

            AggregateResult[] results = [SELECT OpportunityId, SUM(TestAmount__c) RollupAmount FROM OpportunityLineItem WHERE OpportunityId IN :opportunityIds GROUP BY OpportunityId];
            for (AggregateResult result : results) {
                Id opportunityId = (Id) result.get('OpportunityId');
                double rollupAmount = (double) result.get('RollupAmount');

                oppAmounts.put(opportunityId, rollupAmount);
            }

            //Map Amounts for Update
            List<Opportunity> oppsToUpdate = new List<Opportunity>();

            for(Id opportunityId : opportunities.keySet()) {
                Opportunity opp = opportunities.get(opportunityId);

                double rollupAmount = 0;

                if (oppAmounts.containsKey(opportunityId)) {
                    rollupAmount = oppAmounts.get(opportunityId);
                }

                if (rollupAmount != opp.Rollup_Amount__c) {
                    opp.Rollup_Amount__c = rollupAmount;
                    oppsToUpdate.add(opp);
                }
            }

            //Update Opportunities
            if (oppsToUpdate.isEmpty() == false) {
                update oppsToUpdate;
            }
        }
}

 
Flauradel ConsumidoFlauradel Consumido
Thank you. However, I can't seem to have this working :( and constantly receives an error. 

I need to roll up:
From Object: Opportunity
Field: Amount

Where to roll up: 
Object: Commission
Field: Total_Amount__c