0

Getting error isError1 and resultTwo variable does not exist.

public with sharing class erw {
    public boolean isError1{get;set;}
    public String resultTwo;
    AuraEnabled(cacheable=true)
    public static string We(){
        resultTwo='hello';
        isError1=true;
        return resultTwo;
    }
}
1
  • 3
    They do not exist within that context because you are trying to refer to them from a static method when they are non-static properties of your class. If you make them static, you can refer to them in the method. Or, more preferably, you should be able to make both of those variables locals to your method. What does the rest of your class look like and is there a reason to make both of those values properties of the class?
    – nbrown
    Commented Jun 29, 2020 at 12:54

2 Answers 2

4

Your issue is that you've declared your variables as instance variables, and you're trying to access them from a static method.

Static methods do not have access to instance variables. The precise details of why are out of scope for this question, but suffice it to say that this is just how Apex was designed.

You could fix this by adding the static modifier to those variables, but keep in mind that's not the appropriate solution to every situation.

-2

this should resolve your problem

public with sharing class erw {
    
    @AuraEnabled
    public static boolean isError1 {get;set;}
    @AuraEnabled
    public static String resultTwo {get;set;}
    
    @AuraEnabled(cacheable=true)
    public static string We(){
        resultTwo='hello';
        isError1=true;
        return resultTwo;
    }
}
1
  • 5
    The explanation is as important as the solution. Why does this resolve the problem? What is the problem being solved?
    – Derek F
    Commented Jun 29, 2020 at 13:01

You must log in to answer this question.

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