Store related parent record to a Map in Trigger
- johnsontitus
- May 16, 2020
- 1 min read
use case = to store the last opportunity to the account
trigger oppTrg on Opportunity(after insert){
//to map the parent i.e account to the child
Map<Id, Opportunity> accOpp = new Map<Id, Opportunity>();
for(Opportunity op : Trigger.New){
accOpp.put(op.accountId, op);
}
List<Account> accts = [select id, Last_Opp_Name__c from Account where id= :accOpp.keySet()];
for(Account a : accts){
a.Last_Opp_Name__c = accOpp.get(a.id).Name;
}
update accts;
}

Comments