[SalesForce] Variable does not exist error in apex

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;
    }
}

Best Answer

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.

Related Topic