Thursday, September 10, 2020

 

Use Lightning Data Service – No To Apex Controller In Lightning Components

Like Standard Controllers in Visualforce, Salesforce has provided one of the exciting features in Summer 17 is Lightning Data Service.
We can retrieve a record data without writing a single piece of Apex Server side code. We need to use the new force:recordData component to achieve this.

Using this component, we can 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.

Let’s understand how to use force:recordData

Loading a Record:

To load a record using lightning data service ( force:recordData ), we need specify the following:

  •  The ID of the record to load
  • Which component attribute to assign the loaded record
  • A list of fields to load

Syntax:

where,

  •  recordId – the id of the record to load.
  • layoutType –
     FULL – PageLayout of the record ( If there are multiple page layouts for different record types, the pagelayout of the recordid will be taken)
     MINI – Compact Layout
  • targetRecord – where to store the result
  • targetFields – A simplified view of the fields in targetRecord. We generally use this object to refer the fields in our markup
  • targetError – If any error is encountered, it stores the error message.
  • fields – Specifies which of the record’s fields to query.

Either fields or layoutType attribute is needed. If you specify the layoutType, all the fields mentioned in layoutType (FULL or MINI) will be queried. If you specify array of fields ( field attribute ), only the specified fields will be queried.

Saving a record,

To save a record using Lightning Data Service, call saveRecord on the force:recordData component, and pass in a callback function to be invoked after the save operation completes.

In short, we need to do the following to save a record,

  1. Load the record in Edit mode using force:recordData to get the latest version of record.  (All the updates to the record are hold in Edit mode.)
  2. call saveRecord and pass in a callback function to be invoked after the save operation (this callback can be optional)

you need to find the aura:id of force:recordData and call saveRecord to update the record. If we want to perform any operation or to check the status of save operation, pass in a callback function and check the status.

If there is a confusion or not able to understand, I am sure the following example will clear all your doubts.

Example of Lightning Data Service :

Use Case: Create a Lightning Quick Action to display Account Name, Industry and Description using Lightning Data Service. User should be able to update Account Name, Industry and Description and display the records.

What we need to achieve this ?
  1. Lightning Component (typically 3 components)
    1. LightningDataServiceExample – To load the record
    2. LightningEditComponent – To edit the record
    3. LightningContainer  – To toggle base and edit components
  2. Lightning Quick Action on Account Object

Step 1:

Create a component with name LightningDataServiceExample.cmp

LightningDataServiceExampleController.js

Step 2: Create a component to update with name LightningDataServiceEdit

LightningDataServiceEdit.cmp

Add below code in controller  – LightningDataServiceEditController.js

Step 3: Now add the container component to toggle above two components ( base view and edit view ).

LDSContainer.cmp

Step 4: Create a lightning quick action add to the page layout.

( If you are new, check this on how to create a quick action )

Lightning Data Service Quick Action

Step 5: Test the component.

Refresh the Account page and select any record. You will see the magic.

Lightning Data Service

Of course, you can apply better styling to look much better and can add toast message to notify users on update and what not.

Limitations:

  • Lightning Data Service is only available in Lightning Experience and Salesforce1.
  • We can only perform primitive DML operations (create, read, edit and delete ) on only one record. Bulk operations are not supported.
  • Unfortunately, all the objects are not supported, Check this for the list of supported objects.
  • Lightning Data Service is available as a beta. Lightning Data Service isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements.

That’s it for now. Happy Coding !! Cheers !!

1 comment:

  1. Iron Man-Etrian | Titanium Bike, Skate, Bicycle - iTanium Art
    Iron Man is a trademark titanium apple watch of SEGA and was filed on ti89 titanium calculator March titanium tv apk 4, 2016. babylisspro nano titanium spring curling iron This sia titanium trademark was filed on September 5, 2016 in Stock.

    ReplyDelete