Salesforce Lightning Data Services

Lightning Data Service – Standard Controller for Lightning Components

If you have worked on Visualforce and started playing with Lightning Component, you must have identified one important missing feature, which is standard Controller.

Lets assume a scenario, you have two Lightning Components on Account page. One Lightning Component is used to show some fields and other component is used to edit same record.

Problem 1 :

To achieve this, both Lightning components would invoke Apex method separately at the cost of performance, by issuing duplicate server calls. Below image depicts problem, where multiple Lightning component requests same content by making separate server calls.

Lightning Components - Duplicate Server Calls
Lightning Components – Duplicate Server Calls

Problem 2 :

If data gets changed because of any component, Other component needs to be informed with the help of Lightning Events. Again, it would cost performance, as every Lightning component may listen that event.

Problem 3 :

Data Inconsistency. It is quite possible that field updated but because of some defect / issue data not reflected everywhere.

Problem 4 :

Developer needs to write Apex code and handler many scenarios like SOQL, Field level security, writing test classes and many more.

To fix all of above problems, Salesforce introduced Lightning Data Service in Winter 17 as a pilot program and in Summer 17 (currently) its in Beta and expected to be GA in Winter 18.

Advantages of Lightning Data Service

  • No need to write any Apex class
  • No need to write SOQL
  • Field level security and record sharing is inbuilt
  • CRUD operation supported
  • Shared cache is used by all standard and custom components
  • Auto notification to all components
  • Supports offline in Salesforce 1

In summer 17, we can use force:recordData Lightning component to declare Lightning Data Service. Following are important attributes

  • RecordId – represents record to be loaded, deleted or updated. If its null then it indicates record to be inserted
  • LayoutType – Layout from which field needs to be loaded. Currently it supports Full or Compact
  • Fields– If Layout does not contains field, we can use this attribute. Either one of LayoutType or Fields is mandatory.
  • Target* – attributes starting with target indicates values to be returned from Data Service. It can return record or error.
  • Mode – Indicates Data Service should be used in View or Edit mode
  • RecordUpdated – edit mode does not updates record by default, to update target* attributes, use this method handler

Read record using Lightning Data Service

Lets jump to source code, that could be used to read record with the help Lightning Data Service without writing single line of Apex code.

As we can see, force:recordData component is not rendered on user interface.

Update record using Lightning Data Service

Next source code is for Lightning component, which updates record with the help of Data Service.

There are two important observations in above source code.

  1. Saving record in Data Service
    • We have to call saveRecord method on Data Service Lightning Component. Check code on JavaScript controller.
  2. Refresh record in Data Service
    • Use method defined by recordupdate attribute on Data Service. We are using reloadRecord method and code can be seen in JavaScript Controller.

Using Lightning app builder page, we can add these two lightning components on record page. Below image shows complete demo of sample application.

Salesforce Lightning Data Service Demo
Salesforce Lightning Data Service Demo

Limitations of Lightning Data Service

  1. It does not support bulk record operations. (think about StandardSetController functionality in Visualforce)
  2. If record gets updated from outside page, like using Dataloader or some other user, it will not be refreshed. Means, it supports client side caching but not server side caching yet
  3. Not all standard objects are supported (at the time of writing this post)
  4. Its not yet GA

Please find slide of same presentation I gave in Boston developer group meet.

Salesforce Lightning Data Service from Jitendra Zaa





Related Posts


10 responses to “Lightning Data Service – Standard Controller for Lightning Components”

  1. Rocky Avatar

    You did not declare “recordId” and used it on line 14. Trying to understand how it works. Could you explain how it works without declaration?

    1. tarique Shamim Avatar
      tarique Shamim

      Hi @Rocky
      You have to take a look on “force:hasRecordId” in the component declaration section. After implementing “force:hasRecordId”, you will be having access to recordId Attribute.

      Hope this helps your query.

  2. Avi Rosenholtz Avatar
    Avi Rosenholtz

    Hi Jitendra,

    Great post! I am trying to get record data from the parent account of a custom object.

    For an opportunity, this was as simple as defining the fields as “Account.Name” instead of Name and then being able to use in the component as “{!v.accountRecord.Account.Name}”

    When I try this tactic on the custom object Account__c.Name , I get an error “no such column Account__c on entity”

    Any help would be appreciated – Thanks!

    1. Avi Rosenholtz Avatar
      Avi Rosenholtz

      Never mind – I figured it out. You have to use the __r suffix to define and access fields from a related record. Funny how none of the docs explain this simple piece of info!

  3. […] Lightning Data Service – Standard Controller for Lightning Components […]

  4. Bishwambhar Sen Avatar

    Hello Jitendra,

    Nice post. I have an observation though. I realised the delete operation doesn’t auto-notify the components unlike update operation. Event though the developer docs specify that it should auto-notify for delete, it simply doesn’t. Do you think it is a bug? I have to manually redirect the user to the record home page.

  5. Fion Avatar

    Hi Jitendra,

    What is the use of recordUpdated=”{!c.recordUpdated}” ?When does it fire the recordUpdated function?

  6. Fiona Avatar

    Hi Jitendra,
    recordUpdated=”{!c.recordUpdated}” what is the use of this line on the recorddata declaration?


  7. Patel Avatar

    Can you share the related blogs on Lightning Date Service.

  8. vandana Avatar

    can you please share more detail regarding Lightning Data Service

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading