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
solarcookersolarcooker 

SOQL in schedulable class give no row but Developper Consol return good results

Hi there,

I have a problem with my schedulable class for many days. I create a job with this schedulable class to get date field of a custom object and update them, see the code bellow:
global class SellStovesAtResellerJob implements Schedulable {
    global void execute(SchedulableContext sc) {
        //Get the number of days after which stoves delivered to a reseller are sold
        Integer daysUntilSold = Sales_Settings__c.getInstance('Days_Until_Sold').Number_of_Days__c.intValue();
        
        //Calculate the date of the delivery
        Date deliveryDate = System.today() - daysUntilSold;
        
        //Search for all sales to reseller that have made X days ago and before
        List<Sale__c> salesToUpdate = [SELECT Date_of_Delivery__c, Date_of_Sale__c FROM Sale__c
                                       WHERE Date_of_Delivery__c <= :deliveryDate AND Date_of_Sale__c = null];
        
        //Change the date of sale
        for (Sale__c sale : salesToUpdate) sale.Date_of_Sale__c = sale.Date_of_Delivery__c + daysUntilSold;
        
        //Update the sales
        update salesToUpdate;
    }
}
The main problem is that the query returns no row when I look at the log (I create a trace flag for this class) when the job is finished (with success status) while it gives thousand rows in developper consol. I use the code bellow in an Execute Anonymous Windows:
Integer daysUntilSold = Sales_Settings__c.getInstance('Days_Until_Sold').Number_of_Days__c.intValue();
        
        //Calculate the date of the delivery
        Date deliveryDate = System.today() - daysUntilSold;
        
        //Search for all sales to reseller that have made X days ago and before
        List<Sale__c> salesToUpdate = [SELECT Date_of_Delivery__c, Date_of_Sale__c FROM Sale__c
                                       WHERE Date_of_Delivery__c <= :deliveryDate AND Date_of_Sale__c = null];
My user's profil is "System Administrator" with all rights (classes, custom objects and theirs fields, etc ...)

Can somebody explain me what the problem is?

Any kind of help will be welcome.
Cheers


 
Best Answer chosen by solarcooker
solarcookersolarcooker
Hi there,

I found a solution but I don't understand the real problem.
As you can see in the first post, there is no LIMIT clause in the SOQL query and this occurs DML limit or CPU limit exception sometimes. Then I limit result to 10 000 but the SOQL query returns no row (logs in second post). Finally I tried to reduce result to 200 and then 1 000, it worked fine for both.
An improvement using a batch will be the best way but this solution can be used for now.

I don't yet understand why the query didn't return anything and it'll good if someone can give explanation about that.
Cheers.

All Answers

Vivek DVivek D
Can you debug the filters in your SOQL like "deliveryDate". since it is a schedule class may be the values will be different from what you are expecting ?
solarcookersolarcooker
Hello Vivek,
Thanks for your quick response.

Please see bellow a part of the log from the scheduled jobs (in Debug Log)
01:00:00.5 (41303188)|VARIABLE_ASSIGNMENT|[9]|daysUntilSold|30

...

01:00:00.5 (41611675)|VARIABLE_SCOPE_BEGIN|[12]|deliveryDate|Date|false|false
01:00:00.5 (41671712)|VARIABLE_ASSIGNMENT|[12]|deliveryDate|"2016-07-05T00:00:00.000Z"

...

01:00:00.5 (43924705)|SOQL_EXECUTE_BEGIN|[16]|Aggregations:0|SELECT Date_of_Delivery__c, Date_of_Sale__c FROM Sale__c WHERE (Date_of_Delivery__c <= :tmpVar1 AND Date_of_Sale__c = NULL) ORDER BY Date_of_Delivery__c ASC NULLS FIRST LIMIT 10000
01:02:03.610 (123610251422)|CUMULATIVE_LIMIT_USAGE
01:02:03.610 (123610251422)|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

01:02:03.610 (123610251422)|CUMULATIVE_LIMIT_USAGE_END

01:02:03.610 (123610306788)|CODE_UNIT_FINISHED|Sell stoves at reseller
and log from Developper Consol:
08:45:21.1 (4327595)|VARIABLE_ASSIGNMENT|[1]|daysUntilSold|30

...

08:45:21.1 (4846090)|VARIABLE_ASSIGNMENT|[2]|deliveryDate|"2016-07-05T00:00:00.000Z"
08:45:21.1 (4854131)|STATEMENT_EXECUTE|[3]

...

08:45:21.1 (5941846)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|SELECT Date_of_Delivery__c, Date_of_Sale__c FROM Sale__c WHERE (Date_of_Delivery__c <= :tmpVar1 AND Date_of_Sale__c = NULL) ORDER BY Date_of_Delivery__c ASC NULLS FIRST LIMIT 10000
08:45:21.1 (516398940)|SOQL_EXECUTE_END|[3]|Rows:6932
08:45:21.1 (516489154)|HEAP_ALLOCATE|[3]|Bytes:27732
08:45:21.1 (525825037)|HEAP_ALLOCATE|[3]|Bytes:388192
08:45:21.1 (525916383)|HEAP_ALLOCATE|[3]|Bytes:27732
08:45:21.1 (526228326)|VARIABLE_ASSIGNMENT|[3]|salesToUpdate|{"s":1,"v":"List of size 6932 too large to display"}|0x78aa2a9a
08:45:21.526 (526292345)|CUMULATIVE_LIMIT_USAGE
08:45:21.526 (526292345)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 6932 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

08:45:21.526 (526292345)|CUMULATIVE_LIMIT_USAGE_END

08:45:21.1 (526374368)|CODE_UNIT_FINISHED|execute_anonymous_apex
08:45:21.1 (528239373)|EXECUTION_FINISHED
Threre is a little difference with the SOQL query that I give in the first post (ORDER and LIMIT clause) but this is not a problem.
(I can give all rows of the logs if necessary).

Thanks in advance for your help.



 
solarcookersolarcooker
Hi there,

I found a solution but I don't understand the real problem.
As you can see in the first post, there is no LIMIT clause in the SOQL query and this occurs DML limit or CPU limit exception sometimes. Then I limit result to 10 000 but the SOQL query returns no row (logs in second post). Finally I tried to reduce result to 200 and then 1 000, it worked fine for both.
An improvement using a batch will be the best way but this solution can be used for now.

I don't yet understand why the query didn't return anything and it'll good if someone can give explanation about that.
Cheers.
This was selected as the best answer