About Me

My photo
PLANO, Texas, United States

Monday, September 21, 2020

LWC with Salesforce Data

In my previous article, we have learned how SFDX is configured for building LWC and a very basic app HelloLWC, now time to explore more complex things. In this article, I am going to talk about how LWC works with salesforce data. Salesforce provides several ways for Lightning web components to work with data. Always start with the simplest solution and move to a solution that allows for more customization as needed.

  • Use Lightning Data Service (LDS) to Work with Data

  • Use Apex to Work with Data

1) Use Lightning Data Service (LDS) to Work with Data

Lightning Data Service is the preferred (and easiest) way to work with Salesforce data. With Lightning Data Service, developers use JavaScript to connect components to Salesforce data. There are multiple benefits while using LDS:

  • Lightning Data Service maximizes performance. When a record is cached, it’s retrieved from the cache, eliminating unnecessary calls to the server.

  • Lightning Data Service maintains consistent, up-to-date data across multiple components and clients throughout the app lifecycle. If several components use Lightning Data Service to work with a record, and one of them updates the record, the other components automatically reflect that update.

  • It respects CURD Access, FLS, and sharing setting

  • Support all custom and standard object

  • There are two ways to use LDS

    1. Using Base Components-Below are 3 base Lightning components built on Lightning Data Service (LDS)

      1. Lightning-record-form

      2. Lightning-record-view-form

      3. lightning-record-edit-form

    2. Using Wire adapters and functions in the lightning/ui*Api modules

2) Use Apex to Work with Data

So far we learned that LDS is the best approach however, there are some limitations as well. sometimes, neither the lightning-record-*-form components nor the LDS wire adapters and functions suit a particular use case. For example, when you want to customize a single-record data transaction or perform multi-record operations on a single transaction, Apex is the best choice. There are three ways to apex method.

  1. Call Apex Using @wire (Wire a property & Wire a function)

  2. Call a method imperatively

No comments:

Post a Comment