4

So I have a getter, setter and @AuraEnabled method. I know the way this was designed means that you can't have instance variables due to methods being static because of @AuraEnabled. My question is since I get an error message 'variables cannot be found', and my method is long, which is the best way to procede. I could use setParams and callbacks but I have a lot of variables. A lot of the functionality is in the apex method. Is there a better way?

 @AuraEnabled
public static id accidc = Apexpages.currentPage().getParameters().get('id');

When I add static tag theres and error 'AuraEnabled fields cannot be static' however, if its not static then I cannot access the variable in my save method.

    @AuraEnabled
    public static void Save()
   { ...

    CurrentLogdinUser =[SELECT name from User where id = :UserInfo.getUserId()].name; // --- error --- Variable does not exist
   ...
   }

I will also try to add a wrapper for my variables.

6
  • 2
    The best way I see would be to create an inner class as a wrapper. Inside this wrapper, you could declare all your variables with the @AuraEnabled annotation. At init, you instantiate this class and return it to your Lightning component. You can store this variable as an aura:attribute. Inside your lightning component, you can modify some of the variables inside this class. Finally, you send this class to your save method by using JSON.stringify. Commented Sep 11, 2017 at 15:27
  • @MartinLezer No need to stringify, the platform does it for you as appropriate. Also, you could have added this as an answer (I saw your comment after adding my own answer).
    – sfdcfox
    Commented Sep 11, 2017 at 15:33
  • @sfdcfox No problem! My comment wasn't really an answer. Actually I was talking about the fact that he needed to stringify the class if he wanted to pass an instance of its class from javascript to Apex. Are you sure you don't need this? Commented Sep 11, 2017 at 15:48
  • Yes I think I do need this as I am attaching a file and sending an email from save(). The user attaches the file they want and they pass the email address to the apex class
    – M guy
    Commented Sep 11, 2017 at 15:52
  • @MartinLezer Yes, if you had to stringify everything, then passing things like sobjects wouldn't automatically work (hint: it does). If you choose to stringify on the client side, then you have to JSON.deserialize on the server side.
    – sfdcfox
    Commented Sep 11, 2017 at 15:58

1 Answer 1

10

Unlike Visualforce, Lightning doesn't maintain state (a "view state") that is passed to the server. This is part of what makes Lightning... lightning. Passing state back and forth is expensive when it's not necessary, which is what made Visualforce so slow.

If you have a lot of variables, just use a wrapper class:

public class Params {
    @AuraEnabled public String value1;
    @AuraEnabled public String value2;
}
@AuraEnabled public static Params doSomething(Params value) {
    // update value...
    return value;
}

Using this design, you only need to pass a single parameter back and forth, which is a good design pattern anyways.

2
  • Why is it better to wrap them in a class instead of simply leaving them as attributes marked as AuraEnabled? Maybe I didn't understand. Commented Sep 11, 2017 at 15:37
  • 2
    @JavierGarcíaManzano Because the other way is not supported. Read more at AuraEnabled. They specifically state: "Visualforce-style instance properties and getter/setter methods aren’t available."
    – sfdcfox
    Commented Sep 11, 2017 at 15:42

You must log in to answer this question.

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