salesforce lightning data services

How to Use Salesforce Lightning Data Services

INTRODUCTION

Lightning Data Service is a Lightning Component which behaves like a VisualForce Standard Controller in VisualForce Page to provide access to Database to display Data on Page.

We can use Lightning Data Service to Load, Create , Edit or Delete records from Components without Using Apex Controller or Apex Code.

dont miss out iconDon't forget to check: Why and how do you migrate to Salesforce Lightning?

Using Lightning Data Service is much more easier than using server-side Apex Controller ,So whenever possible use Lightning Data Service to Edit or Create Records in Lightning Component

The major  benefit of using LDS is ,Suppose we have multiple components in Single App , Without using LDS each component in the App makes independent call to server even all the components in the App Fetched from same Record Data, which reduces the server call performance , But Record loaded in LDS is shared among all the components in App  because Record is loaded only once and it can be used by all the Components , Using Shared Record by the Components enhanced the consistency of user Interface.

When you have to create a Lightning Application in which you have to create, Edit or Delete records use Lightning Data Service .

How to use Lightning Data Service according to the requirements

Lightning Data Service Provides Four kinds of form functions for different functionalities.

  1. When you have to Load, Create or Edit Records use---> lightning:recordForm
  1. When you have to Display Record only use--->

lightning:recordViewForm with lightning:outputField.

  1. When you have to Create or Edit Records only use--->
    lightning:recordEditForm with lightning:inputField.
  1. When you have to Create, Edit or Delete Records use---> force:recordData.

dont miss out iconCheck out another amazing article by Anuj here: Salesforce Sharing and Security Features.

EXAMPLES.

1.) lightning:recordViewForm with lightning:outputField.

I have a Custom Object Employee__c and I want to display the records of fields of that object in lightning.

<!--RecordViewForm-->

<lightning:recordViewForm recordId="{!v.data }"
    <!---data is a attribute of type String instead of this we can also use Object Id--->
    objectApiName="Employee__c">
    <div class="slds-grid">
        <div class="slds-col slds-size_2-of-3">
            <lightning:outputField fieldName="Name" />
            <lightning:outputField fieldName="Email__c" />
            <lightning:outputField fieldName="Employee_Code__c" />
            <lightning:outputField fieldName="Alternate_Email__c" />
            <lightning:outputField fieldName="Primary_Phone_Number__c" />
            <lightning:outputField fieldName="Alternate_Phone_Number__c" />
            <lightning:outputField fieldName="Department__c" />
        </div>
    </div>
</lightning:recordViewForm>

2.) lightning:recordEditForm with lightning:inputField.

Suppose I have to Create a Form in Which I can enter values in the existing fields here I can simply access the fields of the object and display them with the help of Lightning:recordEditForm.

<!--RecordEditForm-->   
    <lightning:recordEditForm objectApiName="Employee__c"
                              onerror="{!c.handleErrorOnCreate}"
                              onsuccess="{!c.saveEmployeeDetail}">
        
        <div class="slds-grid">
            <div class="slds-col slds-size_1-of-4">
                <lightning:inputField fieldName="Name" required="true"/>
                <lightning:inputField fieldName="Employee_Code__c" required="true"/>
                <lightning:inputField fieldName="Email__c" required="true"/>
                <lightning:inputField fieldName="Primary_Phone_Number__c"/>
                <lightning:inputField fieldName="Alternate_Email__c"/>
                <lightning:inputField fieldName="Alternate_Phone_Number__c"/>
                <lightning:inputField fieldName="Department__c"/>
                <lightning:inputField fieldName="Team__c"/>
                <lightning:inputField fieldName="Password__c" value="1" class="slds-hide"/>
                <div class="slds-align_absolute-center">
                    <lightning:button class="slds-m-top_small" type="submit" label="Create"/>
                </div>
            </div>
        </div>
    </lightning:recordEditForm> 

3.) force:recordData

When You need to perform more operations than above form-functions then use force:recordData.

This is the sample Example of force:recordData how to use it.

<aura:attribute name="record" type="Object" />
<aura:attribute name="simpleRecord" type="Object" />
<aura:attribute name="recordError" type="String" />
<force:recordData aura:id="recordEditor"
        layoutType="COMPACT"
        recordId="{!v.recordId}"
        targetError="{!v.recordError}"
        targetRecord="{!v.record}"
        targetFields="{!v.simpleRecord}"
        mode="EDIT"/>
<!--your code--->

Popular Salesforce Blogs