Leveraging Lightning Data Service in Lightning Framework

Use Lightning Data Service to load, create, edit, or delete a record in your component without requiring Apex code. Lightning Data Service handles sharing rules and field-level security for you. In addition to not needing Apex, Lightning Data Service improves performance and user interface consistency.

At the simplest level, you can think of Lightning Data Service as the Lightning Components version of the Visualforce standard controller. While this statement is an over-simplification, it serves to illustrate a point. Whenever possible, use Lightning Data Service to read and modify Salesforce data in your components.

Data access with Lightning Data Service is simpler than the equivalent using a server-side Apex controller. Read-only access can be entirely declarative in your component’s markup. For code that modifies data, your component’s JavaScript controller is roughly the same amount of code, and you eliminate the Apex entirely. All your data access code is consolidated into your component, which significantly reduces complexity.

Below is a quick example to demonstrate usage of LDS to fetch the account name from the client side without having to actually making server side round trip. This name is used in the client side controller to open a tableau report specific to this account.


<aura:component implements="flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,force:hasRecordId">

<aura:attribute name="recordId" type="String" />

<aura:attribute name="record" type="Object" />

<aura:attribute name="accRecord" type="Object" />

  <!-- Handling it onchange of attribute instead 
  on init because force:recorddata isn't fetching data onload-->

  <aura:handler name="change" value="{!v.accRecord}" 

  <force:recordData recordId="{!v.recordId}"



           fields="Id, Name"/>  


//controller Js code 

    onsimpleRecordAttrChange: function(cmp, evt) {

    console.log("childAttr has changed");

    console.log("old value: " + evt.getParam("oldValue"));
    // Checking the current accRecord attribute value
    console.log("current value: " + evt.getParam("value"));

    var accName = cmp.get("v.accRecord.Name");

    console.log("Company name is :"+accName);
   // Added a dummy url please replace with neccesary url
    var url = 'https:someDefaultUrl?:iid=1&Company='+encodeURIComponent(accName.trim()); 

    //Opening url in new window

    window.open(url );

    //Closing the quick action modal window after open the url in new window

    var dismissActionPanel = $A.get("e.force:closeQuickAction");




Feel free to comment and make any suggestions for improvement. Hope this helps someone struggling to understand the basic concept of LDS.


By H. Rajesh Mudunuri, Docmation