Create a Server-side Apex Controller Class
Introduction
The Lightning Component framework is a UI framework for developing dynamic web apps for mobile and desktop devices.
In this Quick Start, you'll create a simple Lightning Component that renders a list of Contacts from your org. You'll start by creating an Apex controller class, then create a Lightning Component and an event handler and finally render the list of Contacts in the component.
Follow Along with Trail Together
Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.
Build an Apex Controller
Create a class to access data from Contacts.
- In your Trailhead Playground, click the setup gear
and select Developer Console.
- Select File | New | Apex Class.
- For the class name, enter
MyContactListController
and then click OK. - In the body of the class (i.e. between the {} braces), enter the following code Tip: You can copy and paste any code snippet from Trailhead by clicking Copyin the top right corner.
@AuraEnabled public static List<Contact> getContacts(Id recordId) { return [SELECT Id, FirstName, LastName, Email, Phone FROM Contact WHERE AccountId = :recordId]; }
Apex methods designated @AuraEnabled are able to be called from a Lightning component. - Select File | Save.