Newer Version Available

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

Loading a Record

Loading a record can be accomplished entirely in markup using lightning:recordForm. If you need a custom layout, use lightning:recordViewForm. If you need more customization than the form-based components allow for viewing record data, use force:recordData.

Display a Record Using lightning:recordForm

To display a record using lightning:recordForm, provide the record ID and the object API name. Additionally, provide fields using either the fields or layoutType attribute. You can display a record in two modes using the mode attribute.

view
Loads the form using output fields with inline editing enabled. Editable fields have edit icons. If a user clicks an edit icon, editable fields in the form become editable, and the form displays Cancel and Save buttons. This is the default mode when a record ID is provided.
readonly
Loads the form with output fields only. The form doesn’t include edit icons or Cancel and Save buttons.

This example displays an account record in view mode using the compact layout, which includes fewer fields than the full layout. The columns attribute displays the record fields in two columns that are evenly sized. Update the record ID with your own.

1<aura:component>
2    <lightning:recordForm
3        recordId="001XXXXXXXXXXXXXXX"
4        objectApiName="Account"
5        layoutType="Compact"
6        columns="2"/>
7</aura:component>

To display the field values on a record page, implement the flexipage:availableForRecordHome and flexipage:hasRecordId. The component automatically inherits the record ID.

This example displays read-only values for the account’s Name and Industry fields. Add this example to an account record page.

1<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
2    <aura:attribute name="recordId" type="String" />
3    <aura:attribute name="fields" type="String[]" default="['Name','Industry']" />
4    <lightning:recordForm recordId="{!v.recordId}" 
5                          objectApiName="Account"
6                          mode="readonly"
7                          fields="{!v.fields}" />

If you provide both fields and layoutType attributes, the display order of the fields is not guaranteed. To specify the field order, use fields without the layoutType attribute. Alternatively, use the lightning:recordViewForm component as shown in the next section.

Display a Record with a Custom Layout Using lightning:recordViewForm

To display a read-only record with a custom layout, use the lightning:recordViewForm component. To compose a form field, use lightning:outputField components, which maps to a Salesforce field by using the fieldName attribute. Including individual fields lets you style a custom layout using the Lightning Design System utility classes, such as the grid system.

1<aura:component>
2    <lightning:recordViewForm recordId="001XXXXXXXXXXXXXXX" 
3                              objectApiName="Account">
4    <div class="slds-grid">
5        <div class="slds-col slds-size_2-of-3">
6            <lightning:outputField fieldName="Name" />
7            <lightning:outputField fieldName="Phone" />
8        </div>
9        <div class="slds-col slds-size_1-of-3">
10            <lightning:outputField fieldName="Industry" />
11            <lightning:outputField fieldName="AnnualRevenue" />
12        </div>
13    </div>
14</lightning:recordViewForm>
15</aura:component>

If you require more customization when displaying a record than what lightning:recordForm and lightning:recordViewForm allow, consider using force:recordData.

Display Record Data in a Custom User Interface Using force:recordData

force:recordData enables granular customization, including providing your own component to load data. To load a record using Lightning Data Service, add the force:recordData tag to your component and specify:
  • The ID of the record to load
  • A component attribute to assign the loaded record
  • A list of fields to load

To specify a list of fields to load, use 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. The layout depends on the page layout assignment for the profile. For example, if a user using the Marketing User profile is assigned the default account layout, all fields on that layout are available to that user. 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.

We recommend that you use the fields attribute instead of layoutType. Use layoutType only if you want the administrator, not the component, to control the fields that are provisioned. The component must handle receiving every field that is assigned to the layout for the context user.

Note

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.

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 force:recordData. 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
1<aura:component implements="flexipage:availableForRecordHome, force:lightningQuickActionWithoutHeader, force:hasRecordId">
2
3    <aura:attribute name="record" type="Object"/>
4    <aura:attribute name="simpleRecord" type="Object"/>
5    <aura:attribute name="recordError" type="String"/>
6
7    <force:recordData aura:id="recordLoader"
8      fields="Name,BillingCity,BillingState,Industry"
9      recordId="{!v.recordId}"
10      targetFields="{!v.simpleRecord}"
11      targetError="{!v.recordError}"
12      recordUpdated="{!c.handleRecordUpdated}"
13      />
14
15    <!-- Display a lightning card with details about the record -->
16    <div class="Record Details"> 
17    <lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
18        <div class="slds-p-horizontal--small">
19            <p class="slds-text-heading--small">
20                <lightning:formattedText title="Billing City" value="{!v.simpleRecord.BillingCity}" /></p>
21            <p class="slds-text-heading--small">
22                <lightning:formattedText title="Billing State" value="{!v.simpleRecord.BillingState}" /></p>
23        </div>
24    </lightning:card>
25    </div>
26
27    <!-- Display Lightning Data Service errors, if any -->
28    <aura:if isTrue="{!not(empty(v.recordError))}">
29        <div class="recordError">
30            {!v.recordError}</div>
31    </aura:if>
32</aura:component>
When you use the fields attribute, the targetFields attribute returns the record’s Id and SystemModstamp fields, in addition to the fields you requested. In this example, {!v.simpleRecord} returns:
1{
2  "Id":"0011a0000000000000",
3  "Name":"Salesforce",
4  "SystemModstamp":"2020-06-14T23:44:43.000Z",
5  "BillingCity":"San Franscisco",
6  "BillingState":"CA",
7  "Industry":"Technology"
8}
ldsLoadController.js
1({
2    handleRecordUpdated: function(component, event, helper) {
3        var eventParams = event.getParams();
4        if(eventParams.changeType === "LOADED") {
5           // record is loaded (render other component which needs record data value)
6            console.log("Record is loaded successfully.");
7            console.log("You loaded a record in " + 
8                        component.get("v.simpleRecord.Industry"));
9        } else if(eventParams.changeType === "CHANGED") {
10            // record is changed
11        } else if(eventParams.changeType === "REMOVED") {
12            // record is deleted
13        } else if(eventParams.changeType === "ERROR") {
14            // there’s an error while loading, saving, or deleting the record
15        }
16    }
17})

When the record loads or updates, to access the record fields in the JavaScript controller, use the component.get("v.simpleRecord.fieldName") syntax.

force:recordData loads data asynchronously by design since it may go to the server to retrieve data. To track when the record is loaded or changed, use the recordUpdated event as shown in the previous example. Alternatively, you can place a change handler on the attribute provided to targetRecord or targetFields.