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.
How Beneficial
- 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
Limitations
- 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.
'lightning/uiRecordApi'
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
@salesforce/apex/methodName
I hope this blog will give a basic understanding of LDS. In the next blog, we will learn record fetching by UI API.
Also, if you ever need salesforce development services then look no further because Emizentech has a great team of expert salesforce consultants and developers.