Create a Lightning web component called contactList that displays contact names and phone numbers in your app. You get the contact information to display from an Apex class called ContactController.
Add an Apex Class that Queries Contacts
In VS Code, run the command SFDX: Create Project from the Command Palette to create a Salesforce DX project if you don’t have one.
Run SFDX: Open Default Org to log into the org to which you want to add the LWC.
Run SFDX: Refresh SObject Definitions to get completion suggestions for your SObjects related code.
Run SFDX: Create Apex Class and create a class called ContactController.
Next let's add a method called getContacts to this class. Make this method both public and static. Because it’s a static method, you don't need to create an instance of the class to access the method. You can use the name of the class followed by a dot (.) and the name of the method. This method queries a set of fields on the contact object. Paste this code into ContactController.cls:
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts() {
return [
SELECT
Id,
Name,
Email,
Phone
FROM Contact
WITH SECURITY_ENFORCED
LIMIT 10
];
}
}
Next, add the LWC component that displays the contact information.
Create a Component to Display Contact Information
From the Command Palette, run SFDX: Create Lightning Web Component and create a component called contactList in the default location.
In the contactList.html file, cut and paste the following code, then save the file:
Right-click the default folder under force-app/main and run SFDX: Deploy Source to Org to deploy your code to the org.
Add the New Component to Your App in Lightning Experience
In Visual Studio Code, open the Command Palette and run SFDX: Open Default Org.
This command opens your org in a separate browser.
From the App Launcher find and select Sales.
Click Setup gear then select Edit Page.
If you don’t see Edit Page, click the Setup gear and open the setup page. In the search field at the top of the page, type "Home".
In the Home menu, toggle Advanced Seller Home off. Now, go back to your Sales app and reload it. Click the Setup gear icon again. This time, you’ll be able to select the Edit Page option.
Tip
Drag the contactList Lightning web component from the Custom area of the Lightning Components list to the top of the Page Canvas.
Click Save.
Click Activate.
Click Assign as Org Default.
Click Save.
Click Save again, then click Back arrow to return to the page.