1

I have a batch class, In the start method of batch class, I'm calling another class which is returning the query string. When the batch executes I'm getting

System.QueryException: Variable does not exist: IdList exception

IdList, setOfAges and FromDate -> these are global variable.

I'm getting this error on return Database.getQueryLocator(query);

Query Class:

public with sharing class HelperClass{
    public static String getQuery(List<Id> IdList, Set<integer> setOfAges, Date FromDate){
        string query = 'Select Id, Name FROM ObjectA__c WHERE Service__c IN : IdList AND RecordType.Name = \'ABC\' AND Status__c <> \'Cancel\' AND Checkbox__c = FALSE AND (Age_Start_c IN : setOfAges OR Age_End__c IN : setOfAges) AND ((Start_Date__c <: FromDate AND End_Date__c >: FromDate) OR Start_Date__c >=: FromDate)';
    }
}

Batch Class:

global Database.Querylocator start(Database.BatchableContext info){ 
        system.debug('111... '+IdList);
        system.debug('222.. '+setOfAges);

        string query = HelperClass.getQuery(IdList, setOfAges, FromDate);
        return Database.getQueryLocator(query);
}
4
  • Can you post complete code of your batch class? Where IdList declared? is your batch class statefull? May 1, 2018 at 11:05
  • 1
    This is a known issue, you have to have the variable declared in same method or class which are used in SOQL query string.
    – Raul
    May 1, 2018 at 11:06
  • In your start method you used the variable 'IdList' without declaring the variable. May 1, 2018 at 11:06
  • all three IdList, setOfAges and FromDate are global variable and the error I'm getting in return Database.getQueryLocator(query); May 1, 2018 at 12:16

1 Answer 1

3

If IdList is in scope (e.g. is a property of the class) when this executes:

Database.getQueryLocator(query);

I wouldn't expect the error you are getting.

But note that either static (compiled) or dynamic (string) SOQL can be used here and where possible favour static SOQL so the compiler checks the syntax of the query for you. Static SOQL is also easier to format so it is readable.

So if I was writing this method (and assuming the bind variables are in scope) I would write:

// These need to be initialized
private Id[] idList;
private Set<Integer> setOfAges;
private Date fromDate;

public Database.Querylocator start(Database.BatchableContext info) {
    return Database.getQueryLocator([
        Select Id, Name
        FROM ObjectA__c
        WHERE Service__c IN :idList
        AND RecordType.Name = 'ABC'
        AND Status__c <> 'Cancel'
        AND Checkbox__c = FALSE
        AND (
                Age_Start_c IN :setOfAges
                OR
                Age_End__c IN :setOfAges
        )
        AND (
                (Start_Date__c < :fromDate AND End_Date__c > :fromDate)
                OR
                Start_Date__c >= :fromDate
        )
    ]);
}
2
  • When my Query is in start method then I'm getting the results, whereas when I build my query in another class as string and then use it that time I'm getting this error May 1, 2018 at 17:36
  • @salesforceDeveloper As I mention in my answer, what matters is that the variables are in scope when Database. getQueryLocator executes. You can't execute that in a class where the variables are not in scope.
    – Keith C
    May 1, 2018 at 18:06

You must log in to answer this question.

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