2

I have something like this:

@AuraEnabled
public String var1{get;set;}
@AuraEnabled
public Decimal var2{get;set;}
@AuraEnabled
public String var3{get;set;}
@AuraEnabled
public String var4{get;set;}

And I have about 20 variables needed. Is there a way to write this code shorter? Something like this?

@SomeAnnotation
public String var1{get;set;}
public Decimal var2{get;set;}
public String var3{get;set;}
public String var4{get;set;}
1
  • 3
    I'd go with the verbose specification of the variables and the AuraEnabled annotations they need. If you REALLY don't like it, do you have groups of values of the same type that you could consider turning into a small number of arrays? NB: The problem with this approach is meaning is lost when referencing the value, of course, since all you have is a hard-coded index rather than a specific property name...
    – Phil W
    Commented Jan 23, 2020 at 15:59

1 Answer 1

6

You only need to declare @AuraEnabled once per type, if you use the comma operator to define multiple variables. In your case, you could write it as:

@AuraEnabled public String var1, var3, var4;
@AuraEnabled public Decimal var2;

You do not need to add explicit default getter-setter { get; set; } in order for the variables to be accessible (this is a Visualforce-only restriction).

14
  • 2
    sfdcfox, i think setter and getter is needed by lwc, even lwc recipe uses set get. github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/… Commented Jan 23, 2020 at 16:16
  • I can't exactly remember the use case (shame on me) but I had a hard-to-resolve issue last year where adding {get;set;} to properties solved the problem. I am almost sure it was lightning-component related. Commented Jan 23, 2020 at 16:22
  • @SebastianKessel exactly, faced the same countless times. Probably there are some discrepancies in LWC and AURA engine. Commented Jan 23, 2020 at 16:26
  • @PranayJaiswal I just tested this... The getter-setter is not necessary. There may have been a bug last year, but it doesn't seem to be the case now.
    – sfdcfox
    Commented Jan 23, 2020 at 16:30
  • @sfdcfox aha, I see makes sense now. The more you know. I will raise a feature request in lwc recipe to remove setter and getter. Thanks Commented Jan 23, 2020 at 16:34

You must log in to answer this question.

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