• Vanessa Bell 16
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello Everyone!

I have built an Apex Class and Scheduler Class in our FSB2 Full Sandbox Environment. The classes are intended to update all Open Opportunities that have a Days_Stale__c  > 30 and (Today_s_Date__c < TODAY () OR Today_s_Date__c = null) with a value of TODAY() in the Today_s_Date__c Opportunity field. 

When the Today_s_Date__c field is updated to TODAY() on the Opportunity record, it is intended to invoke a Process Builder that updates a "Reminder Date" field with a Date and sends an email to the Opportunity Owner advising the Opporunity is "stale" and needs to be updated.

Process Builder: Opportunity - Email Alerts for Stale Dated Opps 

Batch Apex Class: DailyStaleOppsProcessor
Batch Apex Test Class: DailyStaleOppsProcessorTest
Scheduled Apex Class: DailyStaleOppsscheduledBatchable
Scheduled Apex Test Class: DailyStaleOppsscheduledBatchableTest

When we run the Open Execute Anonymous Window to execute the DailyStaleOppsProcessor apex class, we eventually receive an error saying Apex CPU Time Limit exceeded. 

I have 100% Code Coverage on both classes. I am unsure of how to rewrite my class to stop receiving this error. Here's my code: 

global class DailyStaleOppsProcessor implements Database.Batchable<SObject>, Database.Stateful{
    
    
        List<Opportunity> listRecords = new List<Opportunity>();

    global Database.QueryLocator start (Database.BatchableContext BC)
    {
        String query = 'select id, today_s_date__c from opportunity where isclosed= false and (Days_Stale__c > 30) and (today_s_date__c < today or today_s_date__c = null)' ;
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){

        for(Opportunity obj : (Opportunity []) scope) {
            
        if(obj.Today_s_Date__c!=date.today()){
            obj.Today_s_Date__c = System.today();
            listRecords.add(obj);
            }
         }
     
    }
     
    global void finish(Database.BatchableContext BC){
        system.debug('list to be deleted size  :: '+listRecords.size());
        if(!listRecords.isEmpty())
            {
              update listRecords;
            }

    }
}
Hello Everyone!

I have built an Apex Class and Scheduler Class in our FSB2 Full Sandbox Environment. The classes are intended to update all Open Opportunities that have a Days_Stale__c  > 30 and (Today_s_Date__c < TODAY () OR Today_s_Date__c = null) with a value of TODAY() in the Today_s_Date__c Opportunity field. 

When the Today_s_Date__c field is updated to TODAY() on the Opportunity record, it is intended to invoke a Process Builder that updates a "Reminder Date" field with a Date and sends an email to the Opportunity Owner advising the Opporunity is "stale" and needs to be updated.

Process Builder: Opportunity - Email Alerts for Stale Dated Opps 

Batch Apex Class: DailyStaleOppsProcessor
Batch Apex Test Class: DailyStaleOppsProcessorTest
Scheduled Apex Class: DailyStaleOppsscheduledBatchable
Scheduled Apex Test Class: DailyStaleOppsscheduledBatchableTest

When we run the Open Execute Anonymous Window to execute the DailyStaleOppsProcessor apex class, we eventually receive an error saying Apex CPU Time Limit exceeded. 

I have 100% Code Coverage on both classes. I am unsure of how to rewrite my class to stop receiving this error. Here's my code: 

global class DailyStaleOppsProcessor implements Database.Batchable<SObject>, Database.Stateful{
    
    
        List<Opportunity> listRecords = new List<Opportunity>();

    global Database.QueryLocator start (Database.BatchableContext BC)
    {
        String query = 'select id, today_s_date__c from opportunity where isclosed= false and (Days_Stale__c > 30) and (today_s_date__c < today or today_s_date__c = null)' ;
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){

        for(Opportunity obj : (Opportunity []) scope) {
            
        if(obj.Today_s_Date__c!=date.today()){
            obj.Today_s_Date__c = System.today();
            listRecords.add(obj);
            }
         }
     
    }
     
    global void finish(Database.BatchableContext BC){
        system.debug('list to be deleted size  :: '+listRecords.size());
        if(!listRecords.isEmpty())
            {
              update listRecords;
            }

    }
}