SFDC Stop - Always the latest about Salesforce

Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.

Telegram logo   Join our Telegram Channel

Sunday 10 December 2017

Develop Lightning Components faster using Lightning Data Service

Lightning Data Service allows us to read, create and edit a record without requiring any apex code. There are a number of cool features of Lightning Data Service and I am going to go through some of those features right now:-
  • By using Lightning Data Service you can easily make lightning components that deal with a single record and embed that component to the detail page of that record. 
  • You can even make reusable lightning components that deal with similar data so that they can be reused at multiple record detail pages. 
  • One big advantage of Lightning Data Service is that you don't have to make any apex controller. 
  • Suppose you have two components that work on the same data - one is updating the record and other is displaying the record so as you update the record, the component that is displaying the record is updated automatically and you don't have to take care of any event handling in this case as you normally do in Lightning Components.
  • There are some considerations also that we have to take care while using LDS one of which is that you can only query a single record and that too by Id of that record only while using LDS.
During the starting of this month, I took a session on Lightning Data Service with Noida Salesforce Developer User group at Dreamforce Global Gathering 2017. I have made a presentation for the same and you can view it here and have a more clear idea about the LDS.

During my session, I made multiple lightning components, each for one operation in CRUD. Out of which I gave the demo of read, update and delete operation only but I have the whole code for each operation including create. So let's discuss about each component first and then I'll handover the code to you.

  • Read (LDSComp) - This component displayed the name of the contact.
  • Edit (LDSCompEdit) - This component allows you to edit the contact's first name and last name.
  • Delete (LDSCompDelete) - This component allows you to delete the contact.
  • Create (LDSCompNew) - This component allows you to create a new lead.
There is one more component i.e. LDSWrapperComp in which I wrapped all the components so that I can embed all components together as a single component. The final wrapper component look as follows when you embed it in the record detail page:- 

So, here you can see, I have made four components for each of CRUD operation. What I did is I made this wrapper component reusable. But how ?? As you know that in our Lead as well as Contact both consists of first name and last name fields, so I made a single component and embedded it in both the Lead and Contact detail pages. But as you can see in the last component, I have named it as Create New Lead. This is because, when you create a new record using LDS, you have to give the SObject Type as you load the component initially and I have given my SObject Type as Lead so, in this case whether you embed it in any detail page, it'll create a Lead only.

So let's jump on to the code. You can download the whole code of this LDS demo from here.
If you want to see whole code step by step, you can clone the github repo on your system and then move step by step as I have created 4 different branches in which in each branch one extra component is added to the wrapper component.
You can switch between all 4 branches in the following order:-
  1. read
  2. update
  3. delete
  4. create
More better approach can be if you have setup MavensMate with sublime text to connect with your org, as in that case you have to just create empty components in your actual org and you can go into your org's folder and can set the remote url to my github repo and then switch to particular branch do a git pull and you have the code. But to sync the code to your dev org, you have to make the Lightning Components with same name in your org and then refresh from the server to get those components and then switch to branch and save each file. You may have to delete the .lightning file from the config folder if you are not able to sync the code on saving the file.

If you want to learn how to setup MavensMate you can refer to my other blog post How to setup MavensMate with your favorite text editor for Salesforce.

So, make sure you have tried all the crud operations of LDS by using the code given in the repo. If you have any doubts or problems you can comment below or contact me directly on my twitter handle which is - @rahulcoder


  1. This comment has been removed by a blog administrator.

    1. I am really tired of saying this again and again now. Please don't spam