You need to sign in to do that
Don't have an account?
Jacob Elliott 1
Account Trigger to Update a Field on Related Contacts ends in Apex CPU time limit exceeded
Hey everyone, I have created a trigger to update a field (Last_Changed_Date) on all related Contacts and I am getting the CPU Time out Limit error. I was hoping somoene could take a look and see if there is anyway to optimize?
trigger CCIAllegianceProvider on Account (before update) {
//Map keeps track of accounts that have new addresses
Map<Id, Account> changedProvider = new Map<Id, Account>();
//Trigger.new is a list of Accounts that will be updated
//The loop iterates over the list and adds accounts that have new addresses and adds to changedProvider list
for (Integer i = 0; i < Trigger.new.size(); i++){
if (
(Trigger.old[i].ShippingAddress != Trigger.new[i].ShippingAddress)
|| (Trigger.old[i].BillingAddress != Trigger.new[i].BillingAddress)
|| (Trigger.old[i].Phone != Trigger.new[i].Phone)
|| (Trigger.old[i].V12C__PR_Practice_Tax_ID_Number__c != Trigger.new[i].V12C__PR_Practice_Tax_ID_Number__c)
) {
changedProvider.put(Trigger.old[i].id,
Trigger.new[i]);
}
}
List<Contact> updatedContacts = new List<Contact>();
//Iterate over SOQL query and bind array with in
for (Contact c :[SELECT Id, AccountId, Last_Changed_Date__c FROM Contact WHERE V12C__RecordType__c = 'Provider' AND V12C__Inactive__c = False AND AccountId IN :changedProvider.keySet()]){
Account parentAccount = changedProvider.get(c.AccountId);
c.Last_Changed_Date__c = Datetime.Now();
updatedContacts.add(c);
}
update updatedContacts;
}
trigger CCIAllegianceProvider on Account (before update) {
//Map keeps track of accounts that have new addresses
Map<Id, Account> changedProvider = new Map<Id, Account>();
//Trigger.new is a list of Accounts that will be updated
//The loop iterates over the list and adds accounts that have new addresses and adds to changedProvider list
for (Integer i = 0; i < Trigger.new.size(); i++){
if (
(Trigger.old[i].ShippingAddress != Trigger.new[i].ShippingAddress)
|| (Trigger.old[i].BillingAddress != Trigger.new[i].BillingAddress)
|| (Trigger.old[i].Phone != Trigger.new[i].Phone)
|| (Trigger.old[i].V12C__PR_Practice_Tax_ID_Number__c != Trigger.new[i].V12C__PR_Practice_Tax_ID_Number__c)
) {
changedProvider.put(Trigger.old[i].id,
Trigger.new[i]);
}
}
List<Contact> updatedContacts = new List<Contact>();
//Iterate over SOQL query and bind array with in
for (Contact c :[SELECT Id, AccountId, Last_Changed_Date__c FROM Contact WHERE V12C__RecordType__c = 'Provider' AND V12C__Inactive__c = False AND AccountId IN :changedProvider.keySet()]){
Account parentAccount = changedProvider.get(c.AccountId);
c.Last_Changed_Date__c = Datetime.Now();
updatedContacts.add(c);
}
update updatedContacts;
}
I tried the same code in my org and it didnt give me any error. Do you have many workflow/process which is running while updating the contact because that will also add in CPU time limit exception. But still you are not able to reduce the CPU time limit it is better to go for batch class to update the field "Last_Changed_Date__c " in contact.
The Maximum CPU time on the salesforce servers - 10,000 milliseconds (Synchronous limit) 60,000 milliseconds(Asynchronous limit)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
Regards,
Shweta
All Answers
I tried the same code in my org and it didnt give me any error. Do you have many workflow/process which is running while updating the contact because that will also add in CPU time limit exception. But still you are not able to reduce the CPU time limit it is better to go for batch class to update the field "Last_Changed_Date__c " in contact.
The Maximum CPU time on the salesforce servers - 10,000 milliseconds (Synchronous limit) 60,000 milliseconds(Asynchronous limit)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
Regards,
Shweta
Below is the trailhead which is best for Batchclass. Hope it will be helpful
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch