35

Beginning on page 141 of The Lightning Components Developer Guide, they list the valid types for aura attributes. Apex Classes and collections are included, so I've written an apex controller method to return a collection of Apex Class objects to later save to an attribute..

But when I write the results of calling my method to the javascript console, I see a single element array and the element has no properties other than __proto__. I've seen this work correctly with collections of primitives and SObjects.

Are Apex Classes not actually supported as a return type? I know my @AuraEnabled method is functioning correctly, and my action callback is simply writing the contents of action.getReturnValue() to the console.

0

2 Answers 2

52

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.

3
  • This is exactly the issue. My actual code was returning a collection of ConnectApi.FollowingPage. I initially thought about returning a wrapper for FollowingPage to simply reduce the payload size. I wish I could accept both yours and Peter's answers though. Commented Oct 24, 2014 at 21:35
  • 2
    Any possibility on making some of the standard classes such as SelectOption or ConnectApi.FollowingPage be automatically @AuraEnabled? Right now it is much simpler to just use the JSON serialize and deserialize (two LOC). Commented Oct 25, 2014 at 16:46
  • Also, I found it helpful to add the class as a controller to my aura component. After this, data began to pull through when I called the method inside of the controller.js instead of the blank arrays I was receiving before. Commented Oct 17, 2015 at 14:54
19

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() );
1
  • 5
    This is a working alternative, and as far as I'm concerned the accepted solution until I see an explanation as to why it didn't work out-of-the-box. Thanks Peter! Commented Oct 18, 2014 at 3:26

You must log in to answer this question.

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