Record Data
force:recordData
Provides the ability to create, read, update, and delete Salesforce records.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App
A force:recordData component defines the parameters for accessing, modifying, or creating a record using Lightning Data Service. You have granular control on how you want to display or render the data in your custom component.
We recommend that you use the simpler lightning:recordForm component for accessing, modifying, or creating a record if you don’t need a custom layout or custom rendering of record data.
To load a record, specify its record ID, the component attributes, and a list of fields in your custom component. You can add your custom component to a record home page in the Lightning App Builder, or as a custom action.
In the following example, the record ID is supplied by the implicit recordId attribute added by the force:hasRecordId interface. A lightning:card component is used to display the record data via accountRecord on the targetFields attribute.
1<aura:component implements="force:hasRecordId,flexipage:availableForRecordHome">
2 <aura:attribute name="accountRecord" type="Object"/>
3 <aura:attribute name="recordLoadError" type="String"/>
4
5 <force:recordData aura:id="recordLoader"
6 recordId="{!v.recordId}"
7 fields="Name,Description,Phone,Industry"
8 targetFields="{!v.accountRecord}"
9 targetError="{!v.recordLoadError}"
10 />
11
12 <div>
13 <lightning:card iconName="standard:account" title="{!v.accountRecord.Name}" >
14 <div class="slds-p-horizontal--small">
15 <p class="slds-text-heading--medium"><lightning:formattedPhone title="Phone" value="{!v.accountRecord.Phone}" /></p>
16 <p class="slds-truncate"><lightning:formattedText title="Description" value="{!v.accountRecord.Description}" /></p>
17 <p class="slds-truncate"> <lightning:formattedText title="Industry" value="{!v.accountRecord.Industry}" /></p>
18 </div>
19 </lightning:card>
20 </div>
21</aura:component>Supported Objects
This component doesn’t support all Salesforce standard objects. For example, the Event and Task objects are not supported.
For a list of supported objects, see the User Interface API Developer Guide.
force:recordData loads data asynchronously since it may retrieve data from the server. To track when the record is loaded or changed, use the recordUpdated event or place a change handler on targetRecords or targetFields.
Events
recordUpdated
The event fired when the record is loaded, updated, or deleted.
To see changed field values in a handler for the recordUpdated event, ensure that any field in the fields attribute is also in the page layout. Otherwise, the field changes aren’t available in the handler.
The recordUpdated event returns the following parameters.
| Parameter | Type | Description |
|---|---|---|
| changeType | string | The type of change that fired the event, with values corresponding to “ERROR, “LOADED”, “REMOVED”, or “CHANGED”. |
| changedFields | object | The fields that are updated on the record when the changeType is “CHANGED”. |
For more information, see the Lightning Aura Components Developer Guide.
Attributes
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| body | The body of the component. In markup, this is everything in the body of the tag. | Aura.Component[] | ||
| fields | Specifies which of the record's fields to query. | String[] | ||
| layoutType | Name of the layout to query, which determines the fields included. Valid values are FULL or COMPACT. The layoutType and/or fields attribute must be specified. | String | ||
| mode | The mode in which to load the record: VIEW (default) or EDIT. | String | VIEW | |
| recordId | The record Id | String | ||
| targetError | Will be set to the localized error message if the record can't be provided. | String | ||
| targetFields | A simplified view of the fields in targetRecord, to reference record fields in component markup. | Object | ||
| targetRecord | The provided record. This attribute will contain only the fields relevant to the requested layoutType and/or fields atributes. | Object |
Methods
| Name | Description | Argument Name | Argument Type | Argument Description |
|---|---|---|---|---|
| deleteRecord | Deletes the current record. | callback | Function | Callback function to be invoked after the record has been deleted. |
| getNewRecord | Loads an empty record template into v.targetRecord which includes any predefined default values for the object and record type. | objectApiName | String | The object api name of the record. |
| recordTypeId | String | If null, the default record type for the object will be used. | ||
| skipCache | Boolean | Force a trip to the server, skipping any local cache. | ||
| callback | Function | Callback function to be invoked after new record template is retrieved. | ||
| reloadRecord | Performs the same load function as on init, using current config values (recordId, layoutType, mode, etc) but does not force a server trip if not required. | skipCache | Boolean | Force a trip to the server, skipping any local cache. |
| callback | Function | Callback function to be invoked after record is retrieved. | ||
| saveRecord | Saves the current record. | callback | Function | Callback function invoked after new Record is retrieved |