Newer Version Available

This content describes an older version of this product. View Latest

Returning Data from an Apex Server-Side Controller

Return results from a server-side controller to a client-side controller using the return statement. Results data must be serializable into JSON format.

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

Here’s an example of a controller that returns a collection of custom Apex objects.

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
For example, here’s a wrapper Apex class that contains a few details for an account record. This class is used to package a few details of an account record in a serializable format.
When returned from a remote Apex controller action, the Id and Name properties are defined on the client-side. However, because it doesn’t have the @AuraEnabled annotation, the Phone property isn’t serialized on the server side, and isn’t returned as part of the result data.

Standard Apex limits, such as the maximum number of records retrieved by SOQL queries, apply when returning data from a server-side controller. See Execution Governors and Limits in the Apex Developer Guide.

Note