Data access in lightning components was a slightly complex task when compared to the visualforce pages. The ‘Standard Controller’ gave an advantage to visualforce over lightning apps/components. Finally, Salesforce introduced the ‘Lightning Data Service’ in the winter 17 release and made most of the data access entirely declarative in the component’s markup. In fact, in most cases the component’s javascript controller will be enough to handle the data processing, thereby reducing the apex code.

Lightning Data service is built on a local storage that’s shared across all components that use it. Therefore a record loaded in LDS by one component is available to other components accessing the same record. Caching the loaded records and sharing them across all the components using it not only improves the performance of the components but also improves the data consistency across all the components. When a component modifies/updates a record, the other components using the record are notified, and in most cases, refreshed automatically.

Loading Records using LDS:

This would logically be the first step, loading a record in the LDS cache. This can be accomplished in the component markup itself.

force:recordData is used to fetch the record details from the server. This data can be rendered on the UI using the various UI elements depending on the requirement. In the current example the ID, Name, BillingStreet, and BillingCity are loaded and displayed on the UI without using apex.

Creating a new record using the lightning data service is a special case of loading the data. To create a new record, declare force:recordData without assigning a recordId. In your component’s init or another handler, call the getNewRecord on force:recordData. After assigning the values to the required fields, we call the saveRecord on the force:recordData component, and pass in a callback function to be invoked after the save operation completes. The Lightning Data Service ‘Save’ operation is used in two cases, to update an existing record and to insert a new record.

Read more on LDS.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts


Lightning Web Components – Get Started

Salesforce released the Lightning Web Component framework as part of Spring 19 pre-release. Lightning Web Components is a new programming model for building Lightning components. It uses web standards breakthroughs, can coexist and inter-operate with Read more…


Using Salesforce Reports and Dashboards REST API

The Reports and Dashboards REST API gives you programmatic access to your report and dashboard data as defined in the report builder and dashboard builder. The API lets you integrate the data into any web Read more…


LockerService for Secure Lightning Components

Lightning LockerService is a security architecture for Lightning components, that enhances security by isolating individual Lightning components in their own namespace. LockerService also promotes best practices that improve the supportability of your code by only Read more…