Code By Code – Lightning Framework – Chapter 19 – Lightning Data Service – Loading A Record

Code By Code – Lightning Framework – Chapter 19 – Lightning Data Service – Loading A Record

Chapter 19

Topics for this chapter:

  • Lightning Data Service
  • Loading a Record Using LDS
  • Advantages of LDS

Lightning Data Service

At the simplest level, you can think of Lightning Data Service as the Lightning Components version of the Visualforce standard controller. Use Lightning Data Service to load, create, edit, or delete a record in your component without writing Apex code.

  • LDS also takes care of fields level security and client-side cache behind the scene.
  • Data access with Lightning Data Service is simpler than the equivalent using a server-side Apex controller.
  • Read-Only or Edit access can be entirely declarative in your component’s markup
  • It automatically takes care of your sharing security behind the scene.
  • Read-Only mode will allow read access on the record and you cannot perform any update action.
  • Edit mode will allow to read or update the record.

NOTE: LDS is limited to 1 record for now and multiple record creation and updation is not allowed using LDS, and you still need to write Apex code for these.

Loading A Record Using LDS

Loading a record is the simplest operation in Lightning Data Service. You can accomplish it entirely in the 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

Check out below code snippet from the CarDetails component which loads a new Car record from server:

    <aura:attribute type="Id" name="Id" access="public" />
<aura:attribute type="Car__c" name="car" access="public" />
<aura:attribute name="recordError" type="String" access="private" />
<!-- here we are using lightning data service
to fetch record data
* recordUpdated="{!c.onRecordUpdated}" is optional
and use it only if you need to fire an even on update
* mode can be either EDIT or VIEW-->
<force:recordData aura:id="service"
recordId="{!v.Id}"
mode="EDIT"
targetFields="{!v.car}"
targetError="{!v.recordError}"
recordUpdated="{!c.onRecordUpdated}"
fields="Id, Name, Mileage__c, Per_Day_Rent__c, Build_Year__c, Contact__r.Name,
Contact__r.Email, Contact__r.HomePhone, Car_Type__r.Name, Picture__c" />

RecordId – Id of the record to be loaded
mode – Read-only or Edit mode
targetFields – object attribute to store loaded record’s information
targetError – store error information
fields – fields to fetched from records
layout – if fields are not defined, you can define layout type to load. All fields added on this layout will be loaded. It can be Full or Compact.
recordUpdated – This event will be fired when record will be loaded.

You can use loaded record’s fields in markup with expressions.

{!v.car.Name} or {!v.car.Mileage__c}

Adavntages Of LDS

  • You can get rid of the server-side controller or any apex code if you are doing an operation on a single record.
  • It’s built on highly efficient local storage that’s shared across all components that use it.
  • Records loaded in Lightning Data Service are cached and shared across components.
  • Components accessing the same record see significant performance improvements, because a record is loaded only once, no matter how many components are using it.
  • When one component updates a record, the other components using it are notified, and refresh automatically.

CarDetails Component Code - Load Data Using LDS

Component Markup

Component Controller

What does this code speak?

CarDetail component is the parent component of CarDetail, CarExperience and, AddCarExperience.

It uses Lightning Data Service to load Car record from server. Once the record is loaded, same record is supplied to both child components.

It also has an aura handler to handle application event being fired from CarTile component.

From the controller, it fires the aura method “refresh” of CarExperience child component.

Lightning Component Development Day8 - Lightning Data Service, Lightning Interfaces, App Builder

Test your knowledge

Lightning Data Service

Next In This Guide

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has One Comment

Leave a Reply

Close Menu