Newer Version Available
Using Apex
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.