For Lightning components, Salesforce added a new Feature - Lightning Data Service (LDS) in its Winter ‘17 release. We can use LDS in our Lightning components to perform various CRUD operations like create, read, update or delete a record without the use of any Apex code. This automatically gives us enhanced performance and UI consistency.
As in Visualforce pages, we use ‘Standard Controller’ for basic CRUD operations, similarly, LDS can be used in Lightning components. LDS serves as the ‘data layer’ for Lightning. Without LDS, every Lightning component makes separate calls to the server to fetch the relevant data. This results in reduced performance, and sometimes: inconsistencies in data.
Lightning Data Service identifies all the requests to the server which relate to the same record data and then sends a single shared data request to the server which updates all the relevant components. This method automatically gets rid of data inconsistencies. When one component updates a record, other components using the same record are notified and refreshed. LDS also saves the data in cache so that if the user gets disconnected and is working offline, it automatically syncs the data as soon as the connection is up.
- Minimizing XMLHttpRequests (XHRs).
- Save record data on the client in cache, enabling proactive cache population.
- Eliminates data inconsistencies.
- Gives notifications when a record’s data is changed.
- Sharing a record’s data between different components.
LDS has largely simplified the coding aspect of Lightning components. No Apex controller code is required as all the code used to access the record’s data is consolidated within the LDS component. This results in lowering the complexity of components, and therefore, quality and performance increases.
To use Lightning Data Service in a component, we have to include force:recordData tag. Then, we set the recordId, mode, layoutType or Fields attributes. All these attributes work to specify the record’s ID, the list of fields which need to be queried and the attribute to which this record is to be assigned.
Here, an important point to note is that force:recordData doesn’t provide any UI elements to the component, it is just a simple medium to communicate with the server.
- For now, LDS doesn’t support all the objects. For a list of supported objects, please refer to Lightning Data Service Considerations.
- Lightning Data Service can only be used in Lightning Experience and Salesforce app. It cannot be used in Lightning components for Visualforce pages or other containers.
- LDS only supports one operation in one transaction. To perform more operations in a single transaction, we have to use Apex code.
If you have any query regarding Lightning Data Service or if you want to discuss your requirements regarding the same, please connect with Astrea IT Services at email@example.com