Usually, when we retrieve data from Salesforce org, we use the Apex class where we write the SOQL query. However, this can also be done without using the Apex class.
Instead of Apex, we can use Lightning Data Service and Wire Adapters to get data from the Salesforce org.
Using the above methods, let me show you how to retrieve data from Salesforce in LWC without Apex class.
Get Data From Salesforce in LWC Without Apex
We can retrieve data without an Apex class by using the two methods below:
- Lightning Data Service (LDS).
- Wire Adapters in LWC.
Let me explain each one with examples.
1. Using Lightning Data Services (LDS) in Salesforce
In Salesforce, Lightning Data Service (LDS) provides a declarative approach to interacting with Salesforce data.
It handles record creation, retrieval, update, and deletion (CRUD operations) without writing Apex code.
LDS is perfect for handling standard object data and is fully integrated with the Lightning Experience.
Key Advantages:
- No need to write Apex or manage server-side logic.
- Built-in caching, performance optimization, and record management.
- Fully supports standard objects.
Lightning-Record-Form In Salesforce LWC
The lightning-record-form automatically generates a form for creating, viewing, or editing a record. It’s an easy way to handle standard record forms.
First, we need to create an LWC component in VS code. As you create the component, HTML, JS, and a meta.XML file will be created.
We will not use the Apex class to display the form; instead, we will use the lightning-record-form tag in the HTML file.
In the below script, we display the account object form in view mode. You can also create a record when you click the edit button on the form.
<template>
<lightning-record-form
object-api-name="Account"
record-id={accountId}
layout-type="Full"
mode="View">
</lightning-record-form>
</template>
Then, we need to deploy this component in the Salesforce record or home page. To do that, open the meta.XML file and follow the code below.
To use the component, we need to