You need to sign in to do that
Don't have an account?
Neha 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.
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.
Define your variable as static then you can access it to the finish method. This way you can resolve your issue.
All Answers
Define your variable as static then you can access it to the finish method. This way you can resolve your issue.
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.