public with sharing class SimpleAccountController { @AuraEnabled public static List<SimpleAccount> getAccounts() { // Perform isAccessible() check here // SimpleAccount is a simple "wrapper" Apex class for transport List<SimpleAccount> simpleAccounts = new List<SimpleAccount>(); List<Account> accounts = [SELECT Id, Name, Phone FROM Account LIMIT 5]; for (Account acct : accounts) { simpleAccounts.add(new SimpleAccount(acct.Id, acct.Name, acct.Phone)); } return simpleAccounts; } }
When an instance of an Apex class is returned from a server-side action, the instance is serialized to JSON by the framework. Only the values of public instance properties and methods annotated with @AuraEnabled are serialized and returned.
public class SimpleAccount { @AuraEnabled public String Id { get; set; } @AuraEnabled public String Name { get; set; } public String Phone { get; set; } // Trivial constructor, for server-side Apex -> client-side JavaScript public SimpleAccount(String id, String name, String phone) { this.Id = id; this.Name = name; this.Phone = phone; } // Default, no-arg constructor, for client-side -> server-side public SimpleAccount() {} }