4

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?

1 Answer 1

7

processRecords is static; since the variables are part of an instance, this function can't be static (nor should it be, because it's running in the same instance as the rest of the code). Remove the static modifier, and you should be good to go.

0

You must log in to answer this question.

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