I'm updating the cases field values that are associated to an account using after update trigger. But one account is having 366 cases to it in which I'm facing Apex CPU time limit exceeded exception on updating the account record.
How it works is , when I'm updating "Info" field in Account object , the same field which presents in Case called "Acc_Info" field also gets updated.
Here is my trigger handler code and I'm calling this from an after update trigger.
Map <Id,String> acctToAcctInfo = new Map <Id,String> () ;
Set<Id> acctsWithChangedInfo = new Set<Id> ();
for (Account newAcc : (List<Account>)Trigger.new) {
if ((newAcc.Info__c!= oldAcc.Info__c)) {
acctsWithChangedInfo.add(newAcc.Id) ;
acctToAcctInfo.put(newAcc.Id, newAcc.Info__c) ;
}
}
Map<Id,Case> cMap = new Map<Id,Case>([SELECT Id,AccountId FROM Case WHERE AccountId IN :acctsWithChangedInfo]);
for (Case c: cMap.values()) {
c.Info__c= (acctToAcctInfo.get(c.AccountId);
}
update cMap.values();
Since List will take more CPU time , I have done Map based query here. Please help me on how can I optimize this code and make this work as expected