In this blog, we will learn about lightning data service and use in LWC. Basically every web component needs server call to show data. Now if we are taking the traditional approach suppose we have 3 components in a page and all are showing data about Account.
Now we can see in a single page for the same data there is three apex call and the database will be accessed three times. If data changes by component 1 it will reflect new data while others will have no information about this so they will keep showing the old date. Definitely you don’t want this. So after knowing all this issue we will be happy if we can get a middle man who can fetch data from Salesforce, cached it at his end, and send the data to all components. Also if it can notify all components for any change in data.
So here comes LDS for rescue.
- No need to write Apex Code or SOQL query
- Support most standard and all custom object
- Reduce server load, database load, and network transfer by fetching data once.
- Share cache data across components
- Support field level security and sharing rules so no need to worry about data security and visibility
- Support CRUD operations
- Supports offline in Salesforce1
- Can use in aura as well as LWC
- Not all object supports LDS Check for supported standard object Lightning Aura Components Developer Guide
- Can not use for Vf pages
- No bulk support.
How To Use In LWC?
You can use it in LWC by importing UI API.
After importing this you can use wire adapters and functions.
Also, you can use components like
lightning-record-edit-form, lightning-record-form, and lightning-record-view-form
You can also call your custom APEX controller as
I hope this blog will give a basic understanding of LDS. In the next blog, we will learn record fetching by UI API.