Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Returning Data from an Apex Server-Side Controller
Return data types can be any of the following.
- Simple—String, Integer, and so on. See Basic Types for details.
- sObject—standard and custom sObjects are both supported. See Standard and Custom Object Types.
- Apex—an instance of an Apex class. See Custom Apex Class Types. You can’t use an Apex inner class as a return value for an Apex method that's called by an Aura component.
- Collection—a collection of any of the other types. See Collection Types.
Returning Apex Objects
1public with sharing class SimpleAccountController {
2
3 @AuraEnabled
4 public static List<SimpleAccount> getAccounts() {
5
6 // Perform isAccessible() check here
7
8 // SimpleAccount is a simple "wrapper" Apex class for transport
9 List<SimpleAccount> simpleAccounts = new List<SimpleAccount>();
10
11 List<Account> accounts = [SELECT Id, Name, Phone FROM Account LIMIT 5];
12 for (Account acct : accounts) {
13 simpleAccounts.add(new SimpleAccount(acct.Id, acct.Name, acct.Phone));
14 }
15
16 return simpleAccounts;
17 }
18}When an instance of an Apex class is returned from a server-side action, the framework serializes the return data into JSON format. Only the values of public instance properties and methods annotated with @AuraEnabled are serialized and returned.
These Apex data types are serialized from @AuraEnabled properties and methods. They are supported as Aura component attributes.
- Primitive types except for BLOB
- Objects
- sObjects
- Lists and Maps if they hold elements of a supported type
1public class SimpleAccount {
2
3 @AuraEnabled public String Id { get; set; }
4 @AuraEnabled public String Name { get; set; }
5 public String Phone { get; set; }
6
7 // Trivial constructor, for server-side Apex -> client-side JavaScript
8 public SimpleAccount(String id, String name, String phone) {
9 this.Id = id;
10 this.Name = name;
11 this.Phone = phone;
12 }
13
14 // Default, no-arg constructor, for client-side -> server-side
15 public SimpleAccount() {}
16
17}