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
cchangcchang 

Apex Batch: Scope of Database.QueryLocator returned ignored the where statement in the query

I have a batch job defined below as the scope. 

 

 

global Database.QueryLocator start(Database.BatchableContext BC){

return Database.getQueryLocator([select id, DUNS_Number__c, Parent_DUNS__c, ParentId from Account where Parent_DUNS__c != null]);
}

 

But from the debug log, I saw the scope is returned as: select id, DUNS_Number__c, Parent_DUNS__c, ParentId from Account

The where statement is ignored completely. So there are much more records returned.

I do not know what went wrong.

Saurabh DhobleSaurabh Dhoble

Does your query run fine in the query editor in the developer console ?

cchangcchang
Yes. It runs fine from the IDE schema browser. Means it is a valid SOQL query.
NinoJoseNinoJose

I'm seeing that on other code execution but I doesn't really affect the number of records retrieved.

 

Does it affect your returned value?

cchangcchang
It does affect the number of records returned. With the "where" condition, It is around 1000 records. But it returned about 10000 records with 50 batches.
crop1645crop1645

The APEX 26.0 doc for getQueryLocator() says the argument needs to be a string, not a [..] expression.  Try that

NinoJoseNinoJose

I think you should create a support ticket with Salesforce. I'm currently working on batch apex and although I'm seeing that the query does not include the where condition when I debug, the number of records returned is correct so there might be some internal issue for your Org.

 

Also I have been using expression rather than string specially on old API version and its working for me.

CuriosityCuriosity

I am also facing this same problem and the rows returned are incorrect in my case. This the query which I am passing as a string:

 

String query='Select Id, CreatedDate

                         From InventoryTransaction__c 

                         where Transaction_Type__c Like '%Shipments%' and CreatedDate >= 2012-11-06T12:00:00z  

                         order by CreatedDate Asc limit 10000'';

 

ApexPages.StandardSetController ctl=new ApexPages.StandardSetController(Database.getQueryLocator(query));

 

Is this a Salesforce bug and is there a workaround?