@AuraEnabled Support for Apex Class Return Types?

  • 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.

  • Skip Sauls

    Skip Sauls Correct answer

    7 years ago

    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.

    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.

    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).

    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.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM