function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Apex CPU time limit exceeded on scheduled class


I have the following schedulable class that I want to set up to run on the last day of every month to update a currency field to match another currency field on the same object.  This is done to be able to track monthly reductions and or growth to the currency field being targeted in a report. 

However when I run this class in any sandbox it works just fine, but now when I deployed to production i get the System,LimitExecption: Apex CPU time limt exceeded error for line 13 of my class. 

Any idea why this woudl trigger this error and any ideas on how to resolve?

Thank you so much in advance fo rany help you can provide!

Class code:
global class NightlyStartingCMRRUpdate Implements Schedulable
    global void execute(SchedulableContext sc)
    public void UpdateStartingCMRR()
        List<Zuora__CustomerAccount__c> updates = new List<Zuora__CustomerAccount__c>();
        For(Zuora__CustomerAccount__c CA : [SELECT ID, Zuora__MRR__c, Starting_CMRR__c FROM Zuora__CustomerAccount__c WHERE Zuora__MRR__c != null]){
                CA.Starting_CMRR__c = CA.Zuora__MRR__c;
            update updates;

Shawn Reichner 29Shawn Reichner 29
Update - I added a Map to make the CPU run faster and also added @future to the UpdateStartingCMRR method to allow for more time, and this has seemed to resolve my issue in case anyone else runs up against the same type of error.  Thank you all! 
Manish  ChoudhariManish Choudhari
Hi Shawn,

Yes, Map are used for temp cache purpose and can be your best friend to avoid cpu time limit error. But in your case I believe making UpdateStartingCMRR as future method played a big role since it converted your synchronous transaction to asynchronous transaction.
I know you have already figured it out, just wanted to share my thoughts here. :)