function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neha Arora 50Neha Arora 50 

Error: Variable Does not exist. Global Non-Static Variable does not exist in the batch finish method.

Hello Ohana,

I am getting an error 'Variable does not exist: varName' when I tried to access my global variable in the finish method of the batch class.
Point to Note:
1. Neither I am using a stateful class, not any static variable. So in any case values will be retained in my variable.
2. According to the salesforce article global variables declared in the batch class are accessible in all 3 methods.
Steps already performed:
1. The variable is initiated in the constructor with the parameter passed to the batch class.
2. The same variable is used in the start method to build my query.
Issues:
When I tries to access in finish method got an error saying the variable does not exist.
Below is the code snippet:

global class clsName implements DataBase.Batchable{
  global string var;
  public void clsName(string param){
     this.var = param;
  }
  public Database.queryLocator start(....){
       return Databse.getQueryLocator('SELECT Id from Account where ID =: 'var');
  }
  public static void execute(....){
     //Some Logic
  }
  public static void finish(....){
     System.debug(var);   //Error variable does not exist: var
  }
}

Let me know your suggestion or if I am making any mistake.
Thanks in advance.
Best Answer chosen by Neha Arora 50
Garima Agrawal 31Garima Agrawal 31
@Neha Arora 50

Define your variable as static then you can access it to the finish method. This way you can resolve your issue.
global class clsName implements DataBase.Batchable<sObject>{
  global static string var;
  public void clsName(string param){
     var = param;
  }
  public Database.queryLocator start(Database.BatchableContext bc){
       return Database.getQueryLocator('SELECT Id from Account where ID =: '+var);
  }
  public static void execute(Database.BatchableContext BC, List<SObject> sobjects){
     //Some Logic
  }
  public static void finish(Database.BatchableContext bc){
     System.debug(var);   //Error variable does not exist: var
  }
}

All Answers

Garima Agrawal 31Garima Agrawal 31
@Neha Arora 50

Define your variable as static then you can access it to the finish method. This way you can resolve your issue.
global class clsName implements DataBase.Batchable<sObject>{
  global static string var;
  public void clsName(string param){
     var = param;
  }
  public Database.queryLocator start(Database.BatchableContext bc){
       return Database.getQueryLocator('SELECT Id from Account where ID =: '+var);
  }
  public static void execute(Database.BatchableContext BC, List<SObject> sobjects){
     //Some Logic
  }
  public static void finish(Database.BatchableContext bc){
     System.debug(var);   //Error variable does not exist: var
  }
}
This was selected as the best answer
Neha Arora 50Neha Arora 50
Thanks,
The issue is not only with the static keyword but with this keyword too. Once we define the variable as static we are not needed to use this keyword in the constructre.