Developing Secure Code
Testing Your Apex Code
Making API Calls from Apex
Creating Components in Apex
Testing Components
Previous PDF Versions
Use Apex to write server-side code, such as controllers and test classes. Use Apex only if you need to customize your user interface to do more than what Lightning Data Service allows, such as using a SOQL query to select certain records. Apex provisions data that’s not managed and you must handle data refresh on your own.
Apex controllers handle requests from client-side controllers. For example, a client-side controller might handle an event and call an Apex controller action to persist a record. An Apex controller can also load your record data.
Use Apex in these scenarios:
To work with objects that aren’t supported by User Interface API, such as Task and Event.
To work with operations that User Interface API doesn’t support, like loading a list of records by criteria (for example, to load the first 200 Accounts with Amount > $1M).
To perform a transactional operation. For example, to create an account and create an opportunity associated with the new account. If either create fails, the entire transaction is rolled back.
To call a method imperatively, such as in response to clicking a button, or to delay loading to outside the critical path.
Creating Server-Side Logic with Controllers
The framework supports client-side (JavaScript) and server-side (Apex) controllers. An event is always wired to a client-side controller action, which can in turn call an Apex controller action. For example, a client-side controller might handle an event and call an Apex controller action to persist a record.
Before you can upload a managed package, you must write and execute tests for your Apex code to meet minimum code coverage requirements. Also, all tests must run without errors when you upload your package to AppExchange.
Make API calls from an Apex controller. You can’t make Salesforce API calls from JavaScript code.
Make Long-Running Callouts with Continuations
Use the Continuation class in Apex to make a long-running request to an external web service. Process the response in a callback method. Continuations are the preferred way to manage callouts because they can provide substantial improvements to the user experience.
Creating components on the server side in Apex, using the Cmp.<myNamespace>.<myComponent> syntax, is deprecated. Use $A.createComponent() in client-side JavaScript code instead.