@AuraEnabled Support for Apex Class Return Types?

One thing to check is that the accessors on any objects included in the response include the @AuraEnabled annotation. For example, the following tripped me up recently when adapting sample code for a Lightning application:

@AuraEnabled
public static List<SelectOption> getPuzzleTypes() {
    List<SelectOption> options = new List<SelectOption>();

    Schema.DescribeFieldResult fieldResult = Puzzle__c.Type__c.getDescribe();

    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for (Schema.PicklistEntry f: ple) {
        options.add(new SelectOption(f.getLabel(), f.getValue()));
    }       
    return options;
}

I kept getting empty results, and it was only when I changed the response type that I realized it was a problem with the SelectOption class. This is standard Apex, but it's not Aura-enabled. My own version of it is:

public class SelectOption {
    public SelectOption(String value, String label) {
        this.value = value;
        this.label = label;
        this.disabled = false;
        this.escapeItem = false;
    }

    public SelectOption(String value, String label, Boolean isDisabled) {
        this.value = value;
        this.label = label;
        this.disabled = isDisabled;
        this.escapeItem = false;
    }

    @AuraEnabled
    public String label { get;set; }
    @AuraEnabled
    public String value { get;set; }
    @AuraEnabled
    public Boolean disabled { get;set; }
    @AuraEnabled
    public Boolean escapeItem { get;set; }

}

I'm not sure if this will help with the issue you're facing, but it might be something you can look into.


If you can't get this working, a workaround would be to use JSON to get around the problem, by changing your return type to String and then returning the JSON String of the object.

For example:

If you have in the controller something like:

@AuraEnabled
public static MyClass getMyInstanceOfClass() {
     // do stuff
     MyClass myClassInst = getSomehow();
     // do other stuff
     return myClassInst;
}

Change it to:

@AuraEnabled
public static String getMyInstanceOfClass() {
     // do stuff
     MyClass myClassInst = getSomehow();
     // do other stuff
     return JSON.serialize(myClassInst);
 }

Then in your JS:

var yourClassObj = JSON.parse( action.getReturnValue() );