Work with Salesforce Data

 


In this topic, we will know about different ways to work with Salesforce Data

The easiest way to work with Salesforce data is to use base Lightning components built on Lightning Data Service. If you need more flexibility, use a Lightning Data Service wire adapter directly. Each wire adapter provides different data and metadata. The wire service provisions the data and metadata to your component. Finally, if LDS wire adapters aren’t flexible enough, use Apex.

Base Lightning component built on Lightning Data Service (LDS)

  1. The easiest way to work with Salesforce data is to use base Lightning components built on Lightning Data Service.
  2. Built on LDS are lightning-record-form, lightning-record-edit-form, and lightning-record-view-form
  3. It also takes care of field-level security and sharing for you, so users see only the data that they have access to.
  4. It improves performance because LDS are cached, if someone update record then other component will also get updated automatically

lightning-record-form :- having ability to create, edit, view records and provide Read Only Mode, Layout Types and Multicolumn Layout.

lightning-record-view-form :- Having ability to view records and provide Read Only Mode and Custom Layout for Fields.

lightning-record-edit-form :- Having ability to create, edit records, and provide Custom Layout for Fields.

Mode:- edit, view, readonly
layout-type:- full, compact

Drawback

Does not support all objects (Event and task).

Use LDS wire adapter and function

Wire Adapter :- The wire service provisions an immutable stream of data to the component. Each value in the stream is a newer version of the value that precedes it. It reads data from one of wire adapters lightning/ui*Api modules.

  1. If you need more flexibility, use a Lightning Data Service wire adapter directly. Each wire adapter provides different data and metadata.
  2. To apply business logic on data or create more customizable form use @wire to specify getRecord.
  3. Use to get raw record like picklist, recordInfo, ObjectInfo.
  4. The LDS wire adapters are built on User Interface API resources and live in the lightning/ui*Api modules.
  5.  @wire is reactive, means once data is available it immidiately update it. 
  6. To make property as reactive, we prefix with ‘$’ eg:- ‘$propertyName’.
  7. Various wire adapters lightning/ui*Api.
     a. lightning/uiRecordApi
     b. lightning/uiObjectInfoApi
     c. lightning/uiListsApi
     d. lightning/uiAppApi
     e. lightning/uiRelatedListApi

Drawback

You can use multiple wire adapters to provision data for multiple records, but each operation is an independent transaction. To work with multiple records in a single transaction, use Apex or GraphQL.

Use Apex

if LDS wire adapters aren’t flexible enough, use Apex.

  1. use when you wanna perform operation on Task and Event objects.
  2. use when you need filtration of records eg. amount >= 100.
  3. use to perform transactional operation, either full or partial transactional,

Usage

  1. with @wire.
  2. imperative approach.
  3. with help of Async and await (more cleaner to write code)

When to use imperative approach

  1. To control when the method invocation occurs (for example, in response to clicking a button), call the method imperatively.
  2. To call a method that isn’t annotated with cacheable=true, which includes any method that inserts, updates, or deletes data. 

When we use async and await

  1. When we need to call multiple methods from apex controller class on single call.
  2. We can make apex method chaining.
  3. async function always return promises.
  4. In await function, you’re essentially telling your code to stop and wait for the result of the asynchronous function to complete before going any further.
  5. await pause until the async function is done.


Follow me on linkedIn and Twitter for such more related post

Comments