Lightning applications make it easy to work with data. In this module, you create an Apex controller that allows your Lightning application to retrieve contacts, or to search contacts by name or by id.
In Salesforce, click your name in the upper right corner of the screen. In the dropdown menu, click Developer Console.
In the Developer Console, click File > New > Apex Class. Specify ContactController as the class name and click OK.
Implement the class as follows:
public with sharing class ContactController {
@AuraEnabled
public static List<Contact> findAll() {
return [SELECT id, name, phone FROM Contact LIMIT 50];
}
@AuraEnabled
public static List<Contact> findByName(String searchKey) {
String name = '%' + searchKey + '%';
return [SELECT id, name, phone FROM Contact WHERE name LIKE :name LIMIT 50];
}
@AuraEnabled
public static Contact findById(String contactId) {
return [SELECT id, name, title, phone, mobilephone, Account.Name
FROM Contact WHERE Id = :contactId];
}
}
Click File > Save to save the file