Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Diagram illustrating dynamically loading Salesforce record data using the force:recordData Lightning component
Lightning

Dynamically load record using force:recordData

Load record data dynamically with force:recordData (lightning data service), and run DML without any apex controller.

The short answer

An Aura component can fetch and display record field values through Lightning Data Service and force:recordData, with no Apex controller involved. Set the recordId attribute on force:recordData from JavaScript, then call its reloadRecord() method, and the component loads that record's details on demand.

Key takeaways Use force:recordData to read record field values directly in a Lightning component, with no Apex controller. Assign a new record ID to the recordId attribute of force:recordData with component.find().set(). Call reloadRecord() on the force:recordData instance to refresh the fields for the updated record ID. Bind targetFields to an Aura object attribute so the fetched values can be displayed in markup with lightning:formattedText.

You must be aware about standard controller for visualforce pages, it allows you to access record field values, create or update the record without using apex controller. Similar capability is being provided in custom lightning components using force:recordData which is called lightning data service. You need to pass record id which can be captured by implementing component using force:hasRecordId interface.

This is straight forward, if the component is placed on record page, record id will be available in recordId attribute. Using lightning data service, the field information will be available to use in component. But what if there are list of records and based upon the link click you want to fetch that record information without using APEX controller.

Here I have build a demo lightning component, having a text field to input id of contact record. Once hit on "Fetch Data" button, it will fetch FirstName, LastName, Email fields of that contact. This is all done with the help of lightning data service. No apex controller is involved.

Dynamically fetch contact information

The code, starting with the lightning component.

Lightning Component:

<aura:component >
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="contactRecord" type="Object"/>
    <aura:attribute name="recordLoadError" type="String"/>
    
    <force:recordData aura:id="recordLoader"
                      recordId="{!v.recordId}"
                      fields="FirstName,LastName,Email"
                      targetFields="{!v.contactRecord}"
                      targetError="{!v.recordLoadError}"
                      />
    <lightning:layout multipleRows="true">
        <lightning:layoutItem size="4" padding="around-small">
            <lightning:card class="section">
                <lightning:input label="Enter Contact Id" value="{!v.recordId}"/>
                <div class="slds-text-align_center slds-p-vertical_x-small">
                    <lightning:button label="Fetch Data" title="Fetch Data" onclick="{! c.fetchData }"/>
                </div>
            </lightning:card>	
        </lightning:layoutItem>
        <lightning:layoutItem size="8" padding="around-small">
            <lightning:card class="section">
                First Name: <lightning:formattedText value="{!v.contactRecord.FirstName}" /><br/>
                Last Name: <lightning:formattedText value="{!v.contactRecord.LastName}" /><br/>
                Email: <lightning:formattedText value="{!v.contactRecord.Email}" />
            </lightning:card>
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>
  • force:recordData is used to fetch the field values based upon the id passed by recordId attribute.
  • Input contact record id with the help of lightning:input and store in recordId attribute.
  • Button Fetch Data is calling fetchData method from lightning controller.
  • Another lightning section is used to output the contact information fetched by force:recordData.

Lightning Controller:

({
	fetchData : function(component, event, helper) {
        var RecordId = component.get("v.recordId");
		component.find("recordLoader").set("v.recordId", RecordId);
        component.find("recordLoader").reloadRecord();
	}
})
  • Get the record id inputted by user
  • Set the recordId attribute of force:recordData with newly inputted record id.
  • Refresh the record information as per the new id provided. This is important.

Lightning Style:

.THIS .section{
    padding: 10px;
}

This is not mandatory, just a styling to put padding inside lightning:cards.

Lightning Application:

<aura:application extends="force:slds">
      <c:Dynamic_Data_Service></c:Dynamic_Data_Service>
</aura:application>

This is not mandatory, call the component with the name you have given.

Above logic you can utilize to provide delete functionality in lightning:datatable. I will be having new post on this topic soon.

Frequently asked questions

How do you dynamically load a record using force:recordData?

In your client-side JavaScript controller, set the recordId attribute on the force:recordData instance with component.find().set('v.recordId', recordId). Then call component.find().reloadRecord() to load the record fields without an Apex controller.

Do you need an Apex controller to fetch record fields with force:recordData?

No. force:recordData goes through Lightning Data Service to read, create, or update record field values straight from the component, so no Apex controller is needed.

How do you refresh record data after changing the recordId in force:recordData?

You must call the reloadRecord() method on the force:recordData instance in your JavaScript controller after setting the new record ID.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment