Populate Recent Opportunity Amount on Account

This trigger updates an Account’s “Recent Opportunity Amount” field with the amount of the most recently created Opportunity linked to it. It ensures accounts always reflect the latest Opportunity values.

Practice similar triggers through this YouTube playlist.

Solution:

trigger OpportunityTrigger on Opportunity (after insert) {

            if(Trigger.isInsert){ if(Trigger.isAfter){
                 OpportunityTriggerHandler.populateAmount(Trigger.New);
           }
      }
}
   public class OpportunityTriggerHandler {

           public static void populateAmount(List
                   oppList){ List<Account> accList= new List<Account>(); for(Opportunity
                   opp:oppList){ if(opp.Amount!=null && opp.AccountId!=null){ Account acc =
                   new Account(); acc.Id=opp.AccountId;
                   acc.Recent_Opp_Amount__c=opp.Amount;
                   accList.add(acc);
            }
   }
                           if(!accList.isEmpty()){
                                update accList;
               }
       }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)