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 several base components. To create a form for working with records, use lightning:recordForm, lightning:recordEditForm, or lightning:recordViewForm. If you need more customization than those components allow, consider the force:recordData component.
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
- Display, create, edit, or delete records with granular customization