1

In an apex class, I have a method that I use in a lightning component. In another function I put a value in a parameter, and I want to use this parameter in the method called by lightning :

public object__c num { get; set; } 
public String number {get; set;}
public void reNumber(){
   num = [SELECT number__c FROM object__c WHERE ...];
   number = num.number__c;
}

@AuraEnabled
public static void getName(String id){
   String Name = '-' + number; //this line doesn't compile : I get "variable doesn't exist"
}

So I need to use the value number into the getName method, is there a way to retrieve it ?

1 Answer 1

2

The Apex Server-Side Controller Overview says:

Don't store component state in your controller. Store it in a component's attribute instead.

So you will need to pass the number parameter from the client-side. Or for you case it looks like you can move the query into the getName method (or into a static method you call from it) if it is based on the Id parameter.

Not being able to reference controller instance variables is a core design decision to avoid needing mechanisms like the view state used in Visualforce that can compromise performance and add complexity. This is enforced by the requirement that @AuraEnabled methods must also be static and so are unable to reference controller instance variables.

(@RemoteAction methods and methods in @Restresource classes are also static and so behave similarly.)

You must log in to answer this question.

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