{"id":5796,"date":"2017-07-22T02:28:32","date_gmt":"2017-07-22T02:28:32","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5796"},"modified":"2017-07-22T02:31:01","modified_gmt":"2017-07-22T02:31:01","slug":"lightning-data-services-standard-controller-for-lightning-components","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/lightning-data-services-standard-controller-for-lightning-components\/","title":{"rendered":"Lightning Data Service &#8211; Standard Controller for Lightning Components"},"content":{"rendered":"<p style=\"text-align: justify;\">If you have worked on Visualforce and started playing with\u00a0<strong>Lightning Component<\/strong>, you must have identified one important missing feature, which is\u00a0<strong>standard Controller.<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p><strong>Problem 1 :<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<figure id=\"attachment_6188\" aria-describedby=\"caption-attachment-6188\" style=\"width: 604px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6188\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Lightning-Components-Duplicate-Server-Calls.png?resize=604%2C233&#038;ssl=1\" alt=\"Lightning Components - Duplicate Server Calls\" width=\"604\" height=\"233\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Lightning-Components-Duplicate-Server-Calls.png?w=604&amp;ssl=1 604w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Lightning-Components-Duplicate-Server-Calls.png?resize=300%2C116&amp;ssl=1 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><figcaption id=\"caption-attachment-6188\" class=\"wp-caption-text\">Lightning Components &#8211; Duplicate Server Calls<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p><strong>Problem 2 :<\/strong><\/p>\n<p style=\"text-align: justify;\">If data gets changed because of any component, Other component needs to be informed with the help of\u00a0<strong>Lightning Events.\u00a0<\/strong>Again, it would cost performance, as every Lightning component may listen that event.<\/p>\n<p><strong>Problem 3 :<\/strong><\/p>\n<p style=\"text-align: justify;\">Data Inconsistency. It is quite possible that field updated but because of some defect \/ issue data not reflected everywhere.<\/p>\n<p><strong>Problem 4 :<\/strong><\/p>\n<p style=\"text-align: justify;\">Developer needs to write Apex code and handler many scenarios like SOQL, Field level security, writing test classes and many more.<\/p>\n<p style=\"text-align: justify;\">To fix all of above problems, Salesforce introduced\u00a0<strong>Lightning Data Service\u00a0<\/strong>in Winter 17 as a pilot program and in Summer 17 (currently) its in Beta and expected to be GA in Winter 18.<\/p>\n<p><strong>Advantages of Lightning Data Service<\/strong><\/p>\n<ul>\n<li>No need to write any Apex class<\/li>\n<li>No need to write SOQL<\/li>\n<li>Field level security and record sharing is inbuilt<\/li>\n<li>CRUD operation supported<\/li>\n<li>Shared cache is used by all standard and custom components<\/li>\n<li>Auto notification to all components<\/li>\n<li>Supports offline in Salesforce 1<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">In summer 17, we can use\u00a0<strong>force:recordData\u00a0<\/strong>Lightning component to declare\u00a0<strong>Lightning Data Service.\u00a0<\/strong>Following are important attributes<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>RecordId<\/strong> &#8211; represents record to be loaded, deleted or updated. If its null then it indicates record to be inserted<\/li>\n<li style=\"text-align: justify;\"><strong>LayoutType<\/strong> &#8211; Layout from which field needs to be loaded. Currently it supports <em>Full<\/em> or <em>Compact<\/em><\/li>\n<li style=\"text-align: justify;\"><strong>Fields<\/strong>&#8211; If Layout does not contains field, we can use this attribute. Either one of\u00a0<em>LayoutType\u00a0<\/em>or\u00a0<em>Fields\u00a0<\/em>is mandatory.<\/li>\n<li style=\"text-align: justify;\"><strong>Target*<\/strong> &#8211; attributes starting with\u00a0<em>target\u00a0<\/em>indicates values to be returned from Data Service. It can return record or error.<\/li>\n<li style=\"text-align: justify;\"><strong>Mode<\/strong> &#8211; Indicates Data Service should be used in View or Edit mode<\/li>\n<li style=\"text-align: justify;\"><strong>RecordUpdated<\/strong> &#8211; edit mode does not updates record by default, to update\u00a0<em>target*\u00a0<\/em>attributes, use this method handler<\/li>\n<\/ul>\n<p><strong>Read record using Lightning Data Service<\/strong><\/p>\n<p style=\"text-align: justify;\">Lets jump to source code, that could be used to read record with the help\u00a0<strong>Lightning Data Service<\/strong> without writing single line of Apex code.<br \/>\n<script src=\"https:\/\/gist.github.com\/JitendraZaa\/d1dc71d83cfc9b4776b619a7e7e5aa70.js\"><\/script><\/p>\n<p>As we can see,\u00a0<strong>force:recordData\u00a0<\/strong>component is not rendered on user interface.<\/p>\n<p><strong>Update record using Lightning Data Service<\/strong><\/p>\n<p style=\"text-align: justify;\">Next source code is for Lightning component, which updates record with the help of Data Service.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/b108e7428148fc15a9932c3fab2d1533.js\"><\/script><\/p>\n<p>There are two important observations in above source code.<\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong>Saving record in Data Service<\/strong>\n<ul>\n<li>We have to call\u00a0<strong>saveRecord\u00a0<\/strong>method on Data Service Lightning Component. Check code on JavaScript controller.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Refresh record in Data Service<\/strong>\n<ul>\n<li style=\"text-align: justify;\">Use method defined by\u00a0<strong>recordupdate\u00a0<\/strong>attribute on Data Service. We are using\u00a0<strong>reloadRecord\u00a0<\/strong>method and code can be seen in JavaScript Controller.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Using Lightning app builder page, we can add these two lightning components on record page. Below image shows complete demo of sample application.<\/p>\n<figure style=\"width: 992px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Lightning-Data-Service.gif?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-large\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Lightning-Data-Service.gif?resize=992%2C528&#038;ssl=1\" alt=\"Salesforce Lightning Data Service Demo\" width=\"992\" height=\"528\" \/><\/a><figcaption class=\"wp-caption-text\">Salesforce Lightning Data Service Demo<\/figcaption><\/figure>\n<p><strong>Limitations of Lightning Data Service<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify;\">It does not support bulk record operations. (think about <em>StandardSetController<\/em> functionality in Visualforce)<\/li>\n<li style=\"text-align: justify;\">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<\/li>\n<li style=\"text-align: justify;\">Not all standard objects are supported (at the time of writing this post)<\/li>\n<li style=\"text-align: justify;\">Its not yet GA<\/li>\n<\/ol>\n<p>Please find slide of same presentation I gave in Boston developer group meet.<\/p>\n<p><iframe loading=\"lazy\" style=\"border: 1px solid #CCC; border-width: 1px; margin-bottom: 5px; max-width: 100%;\" src=\"\/\/www.slideshare.net\/slideshow\/embed_code\/key\/hvapv8qeAobxq4\" width=\"595\" height=\"485\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/p>\n<div style=\"margin-bottom: 5px;\"><strong> <a title=\"Salesforce Lightning Data Services\" href=\"\/\/www.slideshare.net\/jitendrazaa\/salesforce-lightning-data-services\" target=\"_blank\" rel=\"noopener\">Salesforce Lightning Data Service<\/a> <\/strong> from <strong><a href=\"https:\/\/www.slideshare.net\/jitendrazaa\" target=\"_blank\" rel=\"noopener\">Jitendra Zaa<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Best Practices for Salesforce Lightning Component. How Lightning Data Service can improve Lightning Component performance and solve inconsistent data problem without writing single line of Apex code. Demo source code, image and slides included.<\/p>\n","protected":false},"author":1,"featured_media":6187,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,9],"tags":[348,311,406],"class_list":["post-5796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apex","category-salesforce","tag-best-practices","tag-lightning-component","tag-summer-17"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Data-Services.png?fit=1045%2C395&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6048,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/15-ways-to-improve-performance-of-lightning-components-in-salesforce\/","url_meta":{"origin":5796,"position":0},"title":"15 ways to improve performance of Lightning Components in Salesforce","author":"Jitendra","date":"May 12, 2018","format":false,"excerpt":"Improve Lightning Component performance using simple 15 rules like Storable Actions, avoiding server trips, Lightning Data Service, Unidirectional data binding, creating component APIs etc","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"15 ways to improve Performance of Lightning Component","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/05\/Screen-Shot-2018-05-12-at-1.31.43-PM.png?fit=1200%2C307&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/05\/Screen-Shot-2018-05-12-at-1.31.43-PM.png?fit=1200%2C307&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/05\/Screen-Shot-2018-05-12-at-1.31.43-PM.png?fit=1200%2C307&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/05\/Screen-Shot-2018-05-12-at-1.31.43-PM.png?fit=1200%2C307&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/05\/Screen-Shot-2018-05-12-at-1.31.43-PM.png?fit=1200%2C307&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":4287,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/learning-lightning-component-trailhead-way\/","url_meta":{"origin":5796,"position":1},"title":"Learning Lightning Component &#8211; Trailhead way","author":"Jitendra","date":"March 19, 2015","format":false,"excerpt":"In Previous article, I\u00a0have introduced what is Trailhead and why employer should start using it to train Salesforce developers and Admins. At the time of writing this article, there are already 16 modules to be learned from it. Recently Salesforce has added some more awesome tutorial\u00a0and \u00a0In this article, I\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Learn Lightning Component Trailhead way","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/03\/Learn-Lightning-Trailhead-way-1024x576.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/03\/Learn-Lightning-Trailhead-way-1024x576.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/03\/Learn-Lightning-Trailhead-way-1024x576.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6176,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/lookup-component-in-salesforce-lightning\/","url_meta":{"origin":5796,"position":2},"title":"Lookup component in Salesforce Lightning","author":"Jitendra","date":"July 6, 2017","format":false,"excerpt":"Salesforce Lightning component in plain JavaScript and SLDS with complete source code","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Lightning Lookup Component","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Lookup-Component.png?fit=978%2C285&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Lookup-Component.png?fit=978%2C285&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Lookup-Component.png?fit=978%2C285&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Lookup-Component.png?fit=978%2C285&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4461,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-started-with-basics-of-lightning-component\/","url_meta":{"origin":5796,"position":3},"title":"Getting started with basics of Lightning Component","author":"Jitendra","date":"May 19, 2015","format":false,"excerpt":"As you might already know\u00a0that next big change in Salesforce is introduction to lightning components. As technology is changing rapidly and to take advantage of cutting edge innovations in web technology, Salesforce doesn't want to stay behind. If we see trend, all major platform has introduced component based design like\u2026","rel":"","context":"In &quot;Lightning&quot;","block_context":{"text":"Lightning","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning\/"},"img":{"alt_text":"Getting started with Lightning component Output","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Getting-started-with-Lightning-component-Output.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":7101,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/implement-infinite-scrolling-for-lightning-data-table\/","url_meta":{"origin":5796,"position":4},"title":"Implement Infinite Scrolling for lightning-datatable","author":"Jitendra","date":"June 10, 2020","format":false,"excerpt":"When to use wire vs Imperative approach to call Apex in Lightning Web Component","rel":"","context":"In &quot;Lightning Web Components&quot;","block_context":{"text":"Lightning Web Components","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning-web-components\/"},"img":{"alt_text":"Salesforce Lightning Datatable Infinite Scrolling","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/06\/Salesforce-Lightning-Datatable-Infinite-Scrolling-e1624579111169.png?fit=700%2C385&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/06\/Salesforce-Lightning-Datatable-Infinite-Scrolling-e1624579111169.png?fit=700%2C385&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/06\/Salesforce-Lightning-Datatable-Infinite-Scrolling-e1624579111169.png?fit=700%2C385&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/06\/Salesforce-Lightning-Datatable-Infinite-Scrolling-e1624579111169.png?fit=700%2C385&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4788,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-radar-chart-lightning-component\/","url_meta":{"origin":5796,"position":5},"title":"Salesforce Lightning Component &#8211; Radar Chart","author":"Jitendra","date":"August 29, 2015","format":false,"excerpt":"How to create Radar chart using Lightning Component in Salesforce with the help of Chart.js library","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Lightning Component for Radar Chart","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/08\/GIF-Radar-Chart.gif?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=5796"}],"version-history":[{"count":6,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5796\/revisions"}],"predecessor-version":[{"id":6195,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5796\/revisions\/6195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/6187"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}