Newer Version Available

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

Passing Data to an Apex Controller

Use action.setParams() in JavaScript to set data to pass to an Apex controller.

This example sets the value of the firstName argument on an Apex controller’s serverEcho method based on the firstName attribute value.

The request payload includes the action data serialized into JSON.

Here's the Apex controller method.

The framework deserializes the action data into the appropriate Apex type. In this example, we have a String parameter called firstName.

Example with Different Data Types

Let's look at an application that sends data of various types to an Apex controller. Each button starts the sequence of passing data of a different type.

Here's the application's JavaScript controller. Each action calls the helper's putdatatype method, which queues up the actions to send to the Apex controller. The method has three parameters:

  1. The component
  2. The Apex method name
  3. The data to pass to the Apex method

The helper has a utility method to send the data to an Apex controller.

Here's the Apex controller.

The pcustomclass() Apex method has a parameter that's a custom Apex type, MyCustomApexClass. Each property in the Apex class must have an @AuraEnabled annotation, as well as a getter and setter.

The MyCustomApexClass Apex class has a property with a type of another custom Apex class, MyOtherCustomApexClass.

You can’t use an Apex inner class as a parameter or return value for an Apex method that's called by an Aura component.

Note