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
Unique TwoUnique Two 

Sum of an opportunity amount field on a custom object help required.

Hello

Because I can't use a roll up summary field how would I get the Sum of Opportunities FIeld Amount on to a Custom Object?
Jayson Faderanga 39Jayson Faderanga 39

Trigger updateSum on Opportunity <after insert, after update, after delete, after undelete>{

List<Opportunity > newRecords = Trigger.new;
List<Opportunity > oldRecords = Trigger.new

if(trigger.isInsert || trigger.isUndelete){

Set<Id> cObjectId = new Set<Id>();

         for(Opportunity newRecord : newRecords){
                  if(newRecord.amount != null){
                            cObjectId.add(newRecord.cObject__c);
                   }
         }
if(!cObjectId.isEmpty() ){

List<cObject__c> cObjects =[select id, sumAmount__c, (select Id, Amount from Opportunities WHERE Amount != null) from cObject__c Where ID IN : cObjectId];
List<cObject__c> coToupdate = new List<cObject__c>();

      if(cObjects != null){

          for(cObject__c co : cObjects){
                for(opportunity o : cObjects.Opportunitnies){
                              co.sumAmount__c += o.Amount;
                  }
            coToUpdate.add(co);
          }

update coToUpdate;
      }
}

}



}

Unique TwoUnique Two
Thank you for this, I am very new to writing a trigger would you mind letting me know where to fill in the details


The object ID is 01I0l0000004PNl
The object name is Commissions

Opportunity {Amount} field to map to Commission Object Field {Settled Loan Value}
 
Jayson Faderanga 39Jayson Faderanga 39
add me on skype, I can walk you through creating one :)

jseal04