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.

    "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) {
                "result: \n" + JSON.stringify(response.getReturnValue(), null, 2) + "\n"
        helper.apexCall(component, action, {}, cb);

Apex Controller cls

public class testApexController {
    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);

TreeNode class

public class TreeNode {
    public String id;

    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


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.

  • ohhh....!! Google ranks this doc 1st, which doesn't tell 2nd scenario in your doc....
    – Xi Xiao
    Apr 6 '18 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
    Apr 6 '18 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
    Apr 6 '18 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
    Apr 6 '18 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
    Apr 6 '18 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.