Can variables with inline get/set not be @AuraEnabled?

I have an existing class that works with a REST API from a managed package. This class represents the payload that the REST API accepts in implementations, so it has a number of variables that use inline get/set methods. I am now updating this class to work with lightning components and am trying to @AuraEnable the member variables so that they can be used in the components. However, when I try to @AuraEnable a member variable that has an inline set defined, I get an internal server error on compile. For example, the following fails to compile with An unexpected error occurred:

global with sharing class MyApiRequest {

  @AuraEnabled
  global Integer myParam {
    get;
    set {
      myParam = value;
      // Other setup code
    }
  }
}

If anyone from the lightning team ends up reading this, the GACK is 2075044-41594 (1492374082).

Is the @AuraEnabled method just not supported for member variables with inline sets?

Answer

As per Salesforce document (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_AuraEnabled.htm) the AuraEnabled is available for methods and also there is another condition which states that AuraEnabled methods should start with a prefix ‘get’ (below is the error message).

AuraEnabled methods must be named with a prefix ‘get’

I tried couple of things to understand more.
To ensure that the setter is visible in Lightning Components, then we should declare the variable with @AuraEnabled and write the getter and setter methods explicitly.

If we have an inline sets with variable declared with @AuraEnabled annotation, this would inherently try to apply the annotation for the setter method. This would violate the above condition of the method with prefix ‘get’.

Below is the sample code which works.

global with sharing class MyApiRequest {
@AuraEnabled global Integer myParam;

global Integer getMyParam() {
    return myParam;
}

global void setMyParam(Integer value) {
    myParam = value;
}

@AuraEnabled
global String myString {get;set;}}

Attribution
Source : Link , Question Author : dsharrison , Answer Author : sfdcfox

Leave a Comment