Newer Version Available

This content describes an older version of this product. View Latest

Lightning Data Service

Use Lightning Data Service to load, create, edit, or delete a record in your component without requiring Apex code. Lightning Data Service handles sharing rules and field-level security for you. In addition to simplifying access to Salesforce data, Lightning Data Service improves performance and user interface consistency.

At the simplest level, you can think of Lightning Data Service as the Lightning components version of the Visualforce standard controller. While this statement is an over-simplification, it serves to illustrate a point. Whenever possible, use Lightning Data Service to read and modify Salesforce data in your components.

Data access with Lightning Data Service is simpler than the equivalent using a server-side Apex controller. Read-only access can be entirely declarative in your component’s markup. For code that modifies data, your component’s JavaScript controller is roughly the same amount of code, and you eliminate the Apex entirely. All your data access code is consolidated into your component, which significantly reduces complexity.

Lightning Data Service provides other benefits aside from the code. It’s built on highly efficient local storage that’s shared across all components that use it. Records loaded in Lightning Data Service are cached and shared across components.

Working with Lightning Data Service in Lightning Web Components? See the Lightning Web Components Developer Guide.

Note

Components accessing the same record see significant performance improvements, because a record is loaded only once, no matter how many components are using it. Shared records also improve user interface consistency. When one component updates a record, the other components using it are notified, and in most cases, refresh automatically.

Creating Components That Use Lightning Data Service

Lightning Data Service is available through force:recordData and several base components. To return raw record data, for example if you need to view or edit only a few fields, and don't need any UI elements or layout information, use force:recordData. When using force:recordData, load the data once and pass it to child components as attributes. This approach reduces the number of listeners and minimizes server calls, which improves performance and ensures that your components show consistent data. For more information, see force:recordData documentation.

To create a form for working with records, use lightning:recordForm, lightning:recordEditForm, or lightning:recordViewForm. One advantage of using the form-based components is that you can achieve many of your record display needs entirely in markup without JavaScript. Another powerful feature of the form-based components is automatic field mapping with field-level validation. The form-based components use a base component that’s appropriate for the field type to render the field automatically.

force:recordData doesn’t include any UI elements; it’s simply logic and a way to communicate to the server. Here are the components that use Lightning Data Service.

lightning:recordForm
Display, create, or edit records
lightning:recordViewForm
Display records with lightning:outputField
lightning:recordEditForm
Create or edit records with lightning:inputField
force:recordData
Create, edit, or delete record data using your own custom UI components