I'm reworking some batch Apex in my org and am having some issues accessing class level variables. An example of what I have is:
global class myBatchClass implements database.batchable<sObject> {
global final map<string, sObject> objMap;
global myBatchClass (){
objMap = new map<string, sObject>();
//iterator to populate map
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('<some_query>');
}
global void execute(Database.BatchableContext BC, sObject[] scope){
system.debug(logginglevel.WARN, '~~~~~~~~~ ' + objMap);
processRecords(scope);
}
global void finish(Database.BatchableContext BC){}
public static void processRecords(sObject[] scope){
system.debug(logginglevel.WARN, '~~~~~~~~~ ' + objMap);
}
}
When trying to save this class, I get errors about Variable does not exist: objMap
in the processRecords
method. If I comment out attempts to access objMap
in that method, the code compiles. If I then execute the batch, I can see the debugs from the execute method.
So, what am I missing? Why can't processRecords()
see the variables that were instantiated/populated by the constructor?