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
Zain ButtZain Butt 

Opportunity Amount field Trigger to Roll-Up from Opportunity Products

Hi, I've been given a task to create an Opportunity Amount field Trigger to Roll-Up from Opportunity Products. I am wondering how I would begin to creating this trigger! Sample code would be great.
LBKLBK
Try this trigger.
 
trigger UpdateOpportunityAmount on OpportunityLineItem (after insert,after update) {
     List<Id> lstOppIDs = new List<Id>();
     for(OpportunityLineItem oli: Trigger.new) {
       lstOppIDs.add(oli.OpportunityId);
     }
     
     Map<Id, Opportunity> mapOpportunity = new Map<Id, Opportunity>([SELECT Id, Name, Amount__c FROM Opportunity WHERE Id =:lstOppIDs]);

     List<Opportunity> lstOpportunities = new List<Opportunity>();
     for(AggregateResult result: [SELECT SUM(TotalPrice) Amt, OpportunityId FROM OpportunityLineItem GROUP BY OpportunityId Having OpportunityId IN :mapOpportunity.keyset()]) { 
        Opportunity opp = mapOpportunity.get((Id)result.get('OpportunityId'));
        opp.Amount__c = (Decimal)result.get('Amt');
        lstOpportunities.add(opp);
     }
     if(lstOpportunities.size() > 0){
        update lstOpportunities;
     }
}
Check out the field names (especially the custom field names I have used) to ensure that they meet your ORG configuration.