Trying to create batch apex where the query has a precedence where clause but I get query exception error, can someone help me?

String country = 'MX';
String stageCold = '99. Cold';
String query = 'SELECT SalesStage__c, ColdSince__c FROM Account WHERE ((ColdSince__c <: Dday.addMonths(-3) AND AccountCountry__c != :country) OR (ColdSince__c <: Dday.addMonths(-2) AND AccountCountry__c = :country))';

1 Answer 1


Dynamic SOQL, like what you have, can only take simple values in variable binds.

Things like AccountCountry__c != :country are fine.
Things like ColdSince__c < :Dday.addMonths(-3) and ColdSince__c < :Dday.addMonths(-2) are problematic.

It doesn't look to me like you need to be using dynamic SOQL here, so turning it into a normal (static) SOQL query (using square braces) is probably the way to go here.

  • But static SOQL is not supported in bacth apex, is it? Commented May 7, 2020 at 15:49
  • 1
    @AdnanZahry No, you can, and should, use inline (static) queries when possible. Database.getQueryLocator accepts either a String or an inline query.
    – sfdcfox
    Commented May 7, 2020 at 15:51
  • 1
    can only take simple values in variable binds - so true and also excludes things like :someOppo.CloseDate -- no dots
    – cropredy
    Commented May 7, 2020 at 16:07
  • thank you guys, I have tried and the code now works as expected Commented May 7, 2020 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .