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
Davide MolinariDavide Molinari 

Query in schedulable class takes too many time

Hi all,
I wrote a schedulable class that takes some quotes in order to delete the related QuoteLineItem records.
I include the part of the code that fails:
global class QuoteExpired implements Schedulable {
    
    global void execute(SchedulableContext ctx){
        checkQuote();
    }
    
    
    global void start(){
        QuoteExpired q = new QuoteExpired();
        String sch = '0 0 5 * * ?';
        system.schedule('DeletionJob', sch, q);
    }
    
    
    public void checkQuote(){
        Date dateThreshold = system.today().addDays(-32);
        System.debug('dateThreshold: ' + dateThreshold);
        
        Set<Id> quoteIds = new Set<Id>();
        Set<Id> oppIds = new Set<Id>();
        Set<Id> qliIds = new Set<Id>();
        Set<Id> bIds = new Set<Id>();
        
        String query = 'Select Id, Data_documento__c, OpportunityId From Quote Where Data_documento__c =N :dateThreshold LIMIT 50000';
        List<Quote> quoteExpired = Database.query(query);

//Other stuff, the execution fails before 

}
The job fails because the query in 'checkQuote()' method takes more than 2 minutes to complete. But it happens only at the first execution of the day: if I execute via Developer Console the same query before job execution, the works fine.
Query results can be 2000 records or 20, depending on 'dateThreshold'.
Here you can find the log of a failure:
33.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:00:17.056 (56665177)|EXECUTION_STARTED
11:00:17.056 (56702924)|CODE_UNIT_STARTED|[EXTERNAL]|01pw0000003m69q|Delete_Basket_Quote_Expired
11:00:17.232 (232827637)|METHOD_ENTRY|[7]|01pw0000003m69q|BasketExpired.BasketExpired()
11:00:17.232 (232855880)|METHOD_EXIT|[7]|BasketExpired
11:00:17.232 (232918417)|METHOD_ENTRY|[10]|01pw0000003m69q|BasketExpired.checkQuote()
11:00:17.233 (233024125)|SYSTEM_METHOD_ENTRY|[22]|System.today()
11:00:17.233 (233074395)|SYSTEM_METHOD_EXIT|[22]|System.today()
11:00:17.233 (233118808)|SYSTEM_METHOD_ENTRY|[22]|com.salesforce.api.interop.apex.bcl.DateMethods.addDays(Integer)
11:00:17.233 (233151947)|SYSTEM_METHOD_EXIT|[22]|com.salesforce.api.interop.apex.bcl.DateMethods.addDays(Integer)
11:00:17.233 (233209770)|SYSTEM_METHOD_ENTRY|[23]|String.valueOf(Object)
11:00:17.233 (233265814)|SYSTEM_METHOD_EXIT|[23]|String.valueOf(Object)
11:00:17.233 (233305967)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY)
11:00:17.233 (233319211)|USER_DEBUG|[23]|DEBUG|dateThreshold: 2015-04-11 00:00:00
11:00:17.233 (233331080)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
11:00:17.233 (233412742)|SYSTEM_CONSTRUCTOR_ENTRY|[24]|<init>(Integer)
11:00:17.233 (233447070)|SYSTEM_CONSTRUCTOR_EXIT|[24]|<init>(Integer)
11:00:17.233 (233477410)|SYSTEM_METHOD_ENTRY|[25]|Set<Date>.add(Object)
11:00:17.233 (233518739)|SYSTEM_METHOD_EXIT|[25]|Set<Date>.add(Object)
11:00:17.233 (233550030)|SYSTEM_CONSTRUCTOR_ENTRY|[27]|<init>(Integer)
11:00:17.233 (233576194)|SYSTEM_CONSTRUCTOR_EXIT|[27]|<init>(Integer)
11:00:17.233 (233597367)|SYSTEM_CONSTRUCTOR_ENTRY|[28]|<init>(Integer)
11:00:17.233 (233622193)|SYSTEM_CONSTRUCTOR_EXIT|[28]|<init>(Integer)
11:00:17.233 (233645180)|SYSTEM_CONSTRUCTOR_ENTRY|[29]|<init>(Integer)
11:00:17.233 (233668071)|SYSTEM_CONSTRUCTOR_EXIT|[29]|<init>(Integer)
11:00:17.233 (233688516)|SYSTEM_CONSTRUCTOR_ENTRY|[30]|<init>(Integer)
11:00:17.233 (233711179)|SYSTEM_CONSTRUCTOR_EXIT|[30]|<init>(Integer)
11:00:17.237 (237082809)|SOQL_EXECUTE_BEGIN|[33]|Aggregations:0|SELECT Id, Data_documento__c, OpportunityId FROM Quote WHERE Data_documento__c = :tmpVar1 LIMIT 50000
11:02:18.762 (121384017951)|CUMULATIVE_LIMIT_USAGE
11:02:18.762|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:02:18.762|CUMULATIVE_LIMIT_USAGE_END

11:02:18.384 (121384053978)|CODE_UNIT_FINISHED|Delete_Basket_Quote_Expired
Can someone please help me?
How can I fix it or avoid this system behavior?

Thanks in advance,
Davide

 
surasura

Hi Davide
I dont think this method is the case for taking too much time , please check execute method is not called inside a loop .time taking to execute a method doesnt differ significantly based on at what time it is called or from  where it is called
Davide MolinariDavide Molinari
Hi sura,
Thanks for reply.
I don't think it is called inside a loop, because I schedule a job on QuoteExpired class, so the loop should be in the code I posted...
surasura
sorry my bad , looking at your debug log it has returned 0 records for your query.
Number of query rows: 0 out of 50000
so are without any records to process is there anything to done in your method
Davide MolinariDavide Molinari
My bad too, I didn't read it.
The query had rows to retrieve, for sure. So it could be that it didn't receive response from server?
Sometimes it happens also via Developer Console....
 
Davide MolinariDavide Molinari
Also because, it takes 2 min to retrieve 0 rows, it is a bit strange....the activity can go ahead also with 0 rows (it just print "0 rows to delete").
surasura
console is not the most reliable tool , it crashes frequnlty , but anyway it is better if you open a case with Salesforce . because if it is a problem with your org only they can fix it . cheers