+ Start a Discussion
Jacob Elliott 1Jacob 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;
}
Best Answer chosen by Jacob Elliott 1
$hwet@$hwet@
Hi Jacob,

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

Jacob Elliott 1Jacob Elliott 1
Thanks so much for the reply Shweta, I am actually researching the batch class right now and it looks like it will work. Thanks for the suggestion!
$hwet@$hwet@
Hi Jacob,

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
This was selected as the best answer
Jacob Elliott 1Jacob Elliott 1
Shweta, I think the problem is the Account I'm using has over 1,000 contacts and it's just too much for a normal trigger. Good ideas!
$hwet@$hwet@
Welcome @Jacob
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
$hwet@$hwet@
Yes! that might be taking a lot of CPU time on the salesforce server. Best way to implement this is Batch class. :)