Files
Riabu_Integration/force-app/main/default/triggers/OpportunityAfterTrigger.trigger
2025-11-26 16:38:23 +05:30

29 lines
964 B
Plaintext

trigger OpportunityAfterTrigger on Opportunity(after update, after insert){
map<Id, List<Decimal>> accIdOppAmtMap = new map<Id, List<Decimal>>();
List<Account> accListToUpdate = new List<Account>();
for(Opportunity opp : Trigger.new){
accIdOppAmtMap.put(opp.accountId, new List<Decimal>());
}
if(accIdOppAmtMap.size() > 0){
List<Opportunity> oppList = [Select AccountId, Amount From Opportunity Where AccountId IN : accIdOppAmtMap.keySet() Order By Amount DESC] ;
for(Opportunity opp : oppList){
if(accIdOppAmtMap.ContainsKey(opp.AccountId)){
accIdOppAmtMap.get(opp.AccountId).add(opp.Amount);
}
}
}
for(Id accId : accIdOppAmtMap.KeySet()){
Account acc = new Account(Id = accId);
if(accIdOppAmtMap.get(accId).size() > 1){
acc.AnnualRevenue = accIdOppAmtMap.get(accId).get(1);
} else{
acc.AnnualRevenue = accIdOppAmtMap.get(accId).get(0);
}
accListToUpdate.add(acc);
}
update accListToUpdate;
}