Share Your Experience With Others

Lightning Data Service

LDS(Lightning Data Service) is similar to the visualforce standard controller, providing access to the data displayed on a page. We can use lightning data source to load,create,edit, or delete a record in your component without requiring Apex code.

Always Remember That Lightning Data Service is used to work on single record

(as i know if i m wrong then please correct me)

Now let’s understand what happens when we don’t use lightning data source:

  • Without LDS, each component within an app makes independent calls to the server to perform CRUD operations on a record, even if all components in the app pull from the same record data.
  • Each server call reduces performance, leaving users twiddling their thumbs instead of working with their data.
  • These independent server calls can also lead to inconsistencies, creating situations where a server call refreshes one component, leaving other components out of date.

Without LDS

Now when we use lightning data source:

Using LDS.png

  • LDS handles sharing rules and FLS for you.
  • 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.
  • Shared records also improve user interface consistency.
  • When one component updates a record, the other components using it are notified, and in most cases, refresh automatically.

Benefits of Lightning Data Service:

  • No APEX
  • No SOQL
  • FLS and sharing built-in
  • Fetch records once, reducing network transfers, app server load, and database server load
  • Cache record data on the client, separate from component metadata
  • Share record data across components
  • Enable progressive record loading, caching, and merging more fields and layouts into the cache
  • Promote consistency by using only one instance of the record data across multiple components
  • Create notifications when record data changes
  • Offline access for Salesforce 1

To get the benefit of Lightning Data Service you need to include in your lightning component like this:

lds.PNG

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
  • recordId : specifies the record to load.
  • mode :
    • EDIT : if something changes on the component, then this won’t result in automatic re-rendering of the component
    • VIEW : if any change to the record is mine whilst we are viewing it, then LDS will auto-render it again.
  • layoutType :
    • FULL : is all the fields defined on the page layout editor
    • COMPACT : is all the fields in the highlights panel at the top of pages in LEX (Lightning Experience).
  • fields : specifies which fields in the record to query.
  • 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.
  • targetFields is automatically updated whenever Lightning Data Service detects a record change.
  • targetError : is populated with any errors

Previously : Using targetRecord

v.targetRecord.fields.Name.value

Now : Using targetFields

v.targetFields.Name 

Using the targetRecord the syntax was too longer and the targetFields simplified it further. Recommend using targetFields and you can drop the tragetRecord. targetRecord is kept only for backward compatibility reasons. Your code will work without using targetRecord.

If we will use targetRecord in our component it will have the relationship and recordType information but targetFields will only have the record information without this information.

Lightning Data Service is continue……………………………………………………………………………….

Leave a comment