Newer Version Available
Lightning Data Service
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.
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