Start tracking your progress
Trailhead Home
Trailhead Home

Create a Server-side Apex Controller Class

Lightning Bolt icon

Attention, Trailblazer!

Salesforce has two different desktop user interfaces: Lightning Experience and Salesforce Classic. This project is designed for Lightning Experience.

You can learn about switching between interfaces, enabling Lightning Experience, and more in the Lightning Experience Basics module here on Trailhead.

Introduction

The Lightning Component framework is a UI framework for developing dynamic web apps for mobile and desktop devices.

Note

Note

As of the Spring ‘19 release (API version 45.0), you can build Lightning components using two programming models: the Lightning Web Components model and the original Aura Components model. Lightning web components and Aura components can coexist and interoperate on a page. This content covers Aura components. For more information about Lightning web components, see Introducing Lightning Web Components.

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.

My Domain Is Already On in Your Trailhead Playground

Do not attempt to turn on My Domain, or change its settings, in your Trailhead Playground. By default, My Domain is already active in every Trailhead Playground.

My Domain name highlighted in a Trailhead Playground URL

In your production org, My Domain lets you create a subdomain unique to your organization. With My Domain, you replace the instance URL that Salesforce assigns you, such as https://na17.lightning.force.com, with your chosen subdomain, for example, https://mydomainname.lightning.force.com.

My Domain is required to create custom Lightning components and set up single sign-on (SSO) in an org. To learn more about My Domain, check out this knowledge article. To learn how to activate it in your production org, see the User Authentication module.

Build an Apex Controller

Create a class to access data from Contacts.

  1. In your Trailhead Playground, click Gear icon to access Setup in Lightning Experience and select Developer Console.

  2. Select File | New | Apex Class.

  3. For the class name, enter MyContactListController and then click OK.

  4. 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 copy in 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.

  5. Select File | Save.

retargeting