0

I'm trying to query to run a Batch but the problem is that Im getting errors with the query string

    public with sharing class ApiLogsPurgeJob implements Database.Batchable<sObject>, Database.Stateful {

    public Date startDate { get; set; }
    public Date endDate { get; set; }
    public String queryString { get;set; }

    public ApiLogsPurgeJob(){
        GeneralSettings__c generalSettings = GeneralSettings__c.getOrgDefaults();
        startDate = generalSettings.StartPurgeDate__c;
        endDate = generalSettings.EndPurgeDate__c;
        System.debug(generalSettings);
        System.debug('startDate: ' + startDate);
        System.debug('endDate: ' + endDate);

        queryString = 'SELECT Id, CreatedDate FROM ApiLog__c' +
                ' WHERE CreatedDate >= '+startDate+' AND CreatedDate <= '+endDate;

    }
    public Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(queryString);
    }

    public void execute(Database.BatchableContext BC, List<sObject> scope) {
        System.debug('Scope: ' + scope);
    }

    public void finish(Database.BatchableContext BC){ }

}

I'm getting this error for the previous syntax:

14:45:11:011 EXCEPTION_THROWN [35]|System.QueryException: line 1:78 no viable alternative at character ' '

I also tryed the query to build as:

queryString = 'SELECT Id, CreatedDate FROM ApiLog__c' +
                ' WHERE CreatedDate >= :startDate AND CreatedDate <= :endDate;

But its not possible since the query has to run insidestart()the:variable` notation will not work outside its method.

How can it be done properly to build the query in the constructor and use it inside the start() since I'm populating it with data from custom settings.

1
  • startDate and endDate are in the class' scope, not the start method's scope. The second method should work just fine (we have code in prod that does something like this).
    – sfdcfox
    Apr 28, 2017 at 18:03

1 Answer 1

1

Actually, those variables will still be in scope and should work just fine.

final Date startDate, endDate;
final String query;
public MyBatch()
{
    // set up above variables
    startDate = ...
    endDate = ...
    query = '... WHERE DAY_ONLY(CreatedDate) >= :startDate AND DAY_ONLY(CreatedDate) <= :endDate';
}
public Database.queryLocator start(...)
{
    // just like query, startDate and endDate are in scope
    return Database.getQueryLocator(query);
}
public void execute(...) { /*implementation*/ }
public void finish(...) { /*implementation*/ }
4
  • The problem is that CreatedDate is in this format 2017-04-28T12:31:40.000Z and startDate: 2017-02-01 00:00:00
    – BoDiE2003
    Apr 28, 2017 at 18:12
  • Testing with WHERE DAY_ONLY(CreatedDate) >= 2017-04-28 using DAY_ONLY works, but how do I turn startDate which is a Date field in a custom setting to have the same format, Ive been trying a couple of things without success
    – BoDiE2003
    Apr 28, 2017 at 18:13
  • @BoDiE2003 Yeah, you can just use DAY_ONLY. Why is that a problem?
    – Adrian Larson
    Apr 28, 2017 at 18:14
  • Setting Date startDate endDate not as Datetime, worked. Thank you, as I debugged and saw that :startDate on system.debug was not replaced, I didnt know if it was working.
    – BoDiE2003
    Apr 28, 2017 at 18:19

You must log in to answer this question.

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