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;
}
}
}