Retrieve Data From Salesforce in LWC Without Apex

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:

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 expose it. To do that, we need to set the isExposed property to true, and in the target tag, you can specify where you want to deploy the component.

<isExposed>true</isExposed>
    <targets>
        <target>lightning__HomePage</target>
        <target>lightning__RecordPage</target>
    </targets>

Then, as you navigate to the Salesforce org, you will see the fields we have in the account object. Also, here, we didn’t use the Apex class to retrieve the account fields even though we can see the fields using the lightning-record-form tag.