4

All my code

Saved as DX project in my github repo

I have a lightning component contains:

  1. client Controller function doInit call Apex Controller getTreefunction and print out received data in console.log
  2. getTree function returns a list of TreeNode class with id and name attributes

My issue is:

I do not think @AuraEnabled is needed for id and name in TreeNode class. However, if I remove @AuraEnabled, they are gone in console.log.

For example, if I remove @AuraEnabled from name in TreeNode class, console.log prints below, without name key:value pair anymore.

result: 
[
  {
    "id": "0016E00000UFOPTQA5"
  }
]

Key code file (all code are saved in my github repo

Client controller js

({
    doInit: function(component, event, helper) {
        var action = component.get("c.getTree");
        var cb = function(response) {
            console.log(
                "result: \n" + JSON.stringify(response.getReturnValue(), null, 2) + "\n"
            );
        };
        helper.apexCall(component, action, {}, cb);
    }
});

Apex Controller cls

public class testApexController {
    @AuraEnabled
    public static List<TreeNode> getTree()
    {
        List<Account> allAccounts = [SELECT Id, Name FROM Account];

        List<TreeNode> tree = new List<TreeNode>();

        addAccounts(tree, allAccounts);
        System.debug('test tree nodes: ' + tree);
        return tree;
    }

    private static void addAccounts(List<TreeNode> tree, List<Account> accounts){
        for (Account a : accounts){
            TreeNode node = new TreeNode(a.id, a.name);
            tree.add(node);
        }
    }
}

TreeNode class

public class TreeNode {
    @AuraEnabled
    public String id;

    @AuraEnabled
    public String name;

    public TreeNode(String id, String name){
        this.id = id;
        this.name = name;
    }
}

All my code

Saved as DX project in my github repo

1 Answer 1

4

Yes, @AuraEnabled is required for attributes to be serialized. This is mentioned in the documentation:

  • Use @AuraEnabled on Apex class static methods to make them accessible as remote controller actions in your Lightning components.

  • Use @AuraEnabled on Apex instance methods and properties to make them serializable when an instance of the class is returned as data from a server-side action.

5
  • ohhh....!! Google ranks this doc 1st, which doesn't tell 2nd scenario in your doc....
    – Xi Xiao
    Commented Apr 6, 2018 at 14:04
  • is there any reason behind this 2nd scenario make them serializable? Security concern? It seems to me a futile thing.
    – Xi Xiao
    Commented Apr 6, 2018 at 14:06
  • @XiXiao I presume it's a performance thing. It allows you to have other data in the class that won't be transferred, reducing bandwidth and memory usage on the client.
    – sfdcfox
    Commented Apr 6, 2018 at 14:08
  • IMHO, that should be up to the developer to decide (wrap needed data in one extra step and send to client) instead of using annotations.. But thanks again man :)
    – Xi Xiao
    Commented Apr 6, 2018 at 14:11
  • 1
    @XiXiao Yeah, I agree. It seems like an unnecessary extra step, but I guess it makes sense if you have a Java background.
    – sfdcfox
    Commented Apr 6, 2018 at 14:28

You must log in to answer this question.

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