Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
1  
This was the problem exactly. Thanks so much! –  veterankamikaze 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.