Newer Version Available

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

Loading a Record

Loading a record is the simplest operation in Lightning Data Service. You can accomplish it entirely in markup.
To load a record using Lightning Data Service, add the force:recordData tag to your component. In the force:recordData tag, specify the ID of the record to be loaded, a list of fields, and the attribute to which to assign the loaded record. force:recordData must specify the following.
  • The ID of the record to load
  • Which component attribute to assign the loaded record
  • A list of fields to load

You can explicitly specify a list of fields to load with the fields attribute. For example, fields="Name,BillingCity,BillingState".

Alternatively, you can specify a layout using the layoutType attribute. All fields on that layout are loaded for the record. Layouts are typically modified by administrators, so layoutType isn’t as flexible as fields when you want to request specific fields. Loading record data using layoutType allows your component to adapt to layout definitions. Valid values for layoutType are FULL and COMPACT.

To access fields that an admin has added to a layout, specify a layoutType. To get a field from an object regardless of whether an admin has included it in a layout, use the fields attribute and request the field by name. To guarantee that a field is always there for the component, specify the field in addition to getting all the fields on a layout.

targetRecord is populated with the current record, containing the fields relevant to the requested layoutType or the fields listed in the fields attribute. targetFields is populated with a simplified view of the loaded record. For example, for the Name field, v.targetRecord.fields.Name.value is equivalent to v.targetFields.Name.

Loading a Record

The following example illustrates the essentials of loading a record using Lightning Data Service. This component can be added to a record home page in the Lightning App Builder, or as a custom action. The record ID is supplied by the implicit recordId attribute added by the force:hasRecordId interface.

ldsLoad.cmp
ldsLoadController.js