Lightning Data Service(LDS) in Salesforce

Lightning Data Service(LDS) will work as data layer in Lightning. It is like Standard Controller in Visualforce page, providing access to data without querying. Use LDS to load, create, edit or delete a record in your component without Apex code. LDS handles sharing rules and field-level security for you.

Without LDS each component make independent server call to perform CRUD operation on a record, even all components are pulling same record data. This independent server calls can also lead to an inconsistencies, lead to situations where a server call refreshes one component leaving other components out of date.

LDS identifies and eliminates requests that invoke the same record data, sending a single shared data request that update all relevant components. Not only does this eliminate inconsistent data between components, it also provide a way to cache the data to work offline in case the user get disconnected and sync the data once connection is restored

So LDS provides reusable Aura components that –

  1. Minimize XMLHttpRequests(XHRs)
  2. Fetch record once, reduce network transfer, app server load and database server load.
  3. Cache record data on the client, separate from component metadata.
  4. Shared record data across components.
  5. Enable progressive record loading, caching and merging more fields and layouts into the cache.
  6. Enable proactive Cache population.
  7. Promote consistency by using only one instance of the record data across multiple components.
  8. Create notification when record data changes.

force:recordData

To use LDS , you have to include force:recordData in your component.

To load record in client side, you have to add force:recordData tag to your component and set your recordId, mode and layout or fields attributes.

  • recordId – specifies the record to load. Record can’t be loaded without a recordId.
  • mode – can be set to either EDIT or VIEW which determines the behaviour of notifications and what operations are available to perform with the record.
  • layout – specifies the layout(FULL or COMPACT) used to display the record, which determines what fields are included. Using layoutType allows your component to adapt to layout definitions.
  • fields – specifies which fields in the record to query.

The fields or layoutType attribute must be provided to load a record’s field data. Since admins usually modify layouts, using fields is a more flexible way to get the fields you need.

The force:recordData tag also supports a set of target* attributes, which are attributes that force:recordData populates itself. The target* attributes can be used to allow access from the UI.

  • targetRecord is populated with the loaded record
  • targetFields is populated with the simplified view of the loaded record
  • targetError is populated with any errors

< force:recordData aura:id="forceRecordCmp"
< !-- aura:id is required to reference the component in your Javascript controller -- >
recordId="{!v.recordId}"
layoutType="{!v.layout}"
fields="{!v.fieldsToQuery}"
mode="VIEW"
targetRecord="{!v.record}"
targetFields="{!v.simpleRecord}"
targetError="{!v.error}"
/ >

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s