Newer Version Available
Passing Data 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:
- The component
- The Apex method name
- 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.