{"id":3384,"date":"2020-06-19T08:26:30","date_gmt":"2020-06-19T13:56:30","guid":{"rendered":"https:\/\/emizentech.com\/blog\/?p=3384"},"modified":"2025-01-02T09:39:56","modified_gmt":"2025-01-02T09:39:56","slug":"how-to-fetch-records-using-lightning-data-service","status":"publish","type":"post","link":"https:\/\/emizentech.com\/blog\/how-to-fetch-records-using-lightning-data-service.html","title":{"rendered":"How To Fetch Records By Lightning Data Service In Salesforce"},"content":{"rendered":"<p>In this blog, we will learn about fetching records by using LDS. In the previous blog, we learned about <a href=\"https:\/\/emizentech.com\/blog\/lightning-data-service-in-lwc.html\">how to use Lightning Data Service<\/a> now let\u2019s get record details by LDS into it. We will use Lightning\/ui*Api Wire Adapters and Functions.<\/p>\n<p>Let\u2019s check step by step<\/p>\n<h3>1. Import this in your js controller.<\/h3>\n<pre>import { LightningElement, wire } from 'lwc';\r\nimport { getRecord } from 'lightning\/uiRecordApi';<\/pre>\n<p>Here <strong>@wire<\/strong> is mandatory because we are going to use a wire adapter. Now use get record function to fetch data.<\/p>\n<pre>\/\/ one way\r\n@wire(getRecord, { \r\n    recordId: string, \r\n    fields: string|string[], \r\n    optionalFields?: string|string[]\r\n    )\r\npropertyOrFunction<\/pre>\n<ul>\n<li><strong>recordId :<\/strong> This will take a unique record id of your salesforce record<\/li>\n<li><strong>fields :<\/strong> This will take the array of fields that you want to retrieve. Since we know that LDS follows FLS if you asked for any field that the user has no permission to a field, an error is returned.<\/li>\n<li><strong>optionalFields :<\/strong> Use this if you do not sure about user permission because if you enter fields in this and user has no permission it will not fetch that field in response but won\u2019t throw any error.<\/li>\n<\/ul>\n<p>There is another way in which we can call this getRecord function and that is by providing layout type while in the above option we have provided fields. The syntax will be :<\/p>\n<pre>\/\/ another way\r\n@wire(getRecord, { \r\n    recordId: string, \r\n    layoutTypes: string|string[],\r\n    modes?: string|string[], \r\n    optionalFields?: string|string[]\r\n    )propertyOrFunction<\/pre>\n<h4>Also Read: <a href=\"https:\/\/emizentech.com\/blog\/database-stateful-in-batch-apex.html\" target=\"_blank\" rel=\"noopener\">How To Use Database.Stateful In Batch Apex<\/a><\/h4>\n<h3>Points To Note<\/h3>\n<p>There are two required arguments for this getRecord call.<br \/>\n1. RecordId<br \/>\n2. Fields\/Layout Type (must provide any one of these)<\/p>\n<p>Example: Let\u2019s create a fetchRecord web component to see what we learned till now.<\/p>\n<h3>fetchRecord.js<\/h3>\n<pre>import {\r\n    LightningElement,\r\n    api,track,\r\n    wire\r\n} from 'lwc';\r\nimport {\r\n    getRecord\r\n} from 'lightning\/uiRecordApi';\r\n\r\n\/\/ const fields = [\r\n\/\/     'Contact.Name',\r\n\/\/     'Contact.Title',\r\n\/\/     'Contact.Phone',\r\n\/\/     'Contact.Email',\r\n\/\/     'Contact.Department',\r\n\/\/ ];\r\n\r\nexport default class FetchRecord extends LightningElement {\r\n    @api recordId;\r\n    @track contactRec;\r\n    \/\/ by providing field in argument. Uncomment this and comment layout type code to check functionality\r\n    \/\/ @wire(getRecord, {\r\n    \/\/     recordId: '$recordId',\r\n    \/\/     fields\r\n    \/\/ })\r\n    \/\/By providing layout type in argument\r\n    @wire(getRecord, {\r\n        recordId: '$recordId',\r\n        layoutTypes: ['Full']\r\n    })\r\n        contactRec;\r\n\r\n    get department(){\r\n        return this.contactRec.data.fields.Department.value;\r\n    }\r\n    get name() {\r\n        console.log(this.contactRec.data.fields);\r\n     \/\/ If you use field option to fetch record you can directly access Name by using\r\n    \/\/  this.contactRec.data.fields.Name.value since we are specifying this in field array\r\n        var name = this.contactRec.data.fields.FirstName.value  +' ' +this.contactRec.data.fields.LastName.value;\r\n        return name;\r\n    }\r\n\r\n    get title() {\r\n        return this.contactRec.data.fields.Title.value;\r\n    }\r\n\r\n    get phone() {\r\n        return this.contactRec.data.fields.Phone.value;\r\n    }\r\n\r\n    get email() {\r\n        return this.contactRec.data.fields.Email.value;\r\n    }\r\n}<\/pre>\n<div class=\"center-imgs\"><a href=\"https:\/\/emizentech.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2389 size-full\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" width=\"1000\" height=\"200\" \/><\/a><\/div>\n<h3>fetchRecord.html<\/h3>\n<pre>&lt;template&gt;\r\n    &lt;lightning-card title=\"Lightning Data Service\"&gt;\r\n        &lt;template if:true={contactRec.data}&gt; \r\n            &lt;div class=\"slds-m-around_medium\"&gt;\r\n                &lt;p&gt;{name}&lt;\/p&gt;\r\n                &lt;p&gt;{title}&lt;\/p&gt;\r\n                &lt;p&gt;\r\n                    &lt;lightning-formatted-phone value={phone}&gt;&lt;\/lightning-formatted-phone&gt;\r\n                &lt;\/p&gt;\r\n                &lt;p&gt;\r\n                    &lt;lightning-formatted-email value={email}&gt;&lt;\/lightning-formatted-email&gt;\r\n                &lt;\/p&gt;\r\n                &lt;p&gt;\r\n                    &lt;lightning-formatted-text value={department}&gt;&lt;\/lightning-formatted-text&gt;\r\n                &lt;\/p&gt;\r\n            &lt;\/div&gt;\r\n        &lt;\/template&gt;\r\n    &lt;\/lightning-card&gt;\r\n&lt;\/template&gt;<\/pre>\n<p>Read the comment section in js file carefully and try by providing field option too for better understanding.<\/p>\n<p>I hope this will help. Next, we will learn about Record Creation using LDS.<\/p>\n<p>We at Emizentech have expertise in providing <a href=\"https:\/\/emizentech.com\/salesforce.html\">salesforce development services<\/a> for almost all of the salesforce products. If you ever need assistance get in touch with our <a href=\"https:\/\/emizentech.com\/salesforce-consulting.html\">salesforce consultants and developers<\/a>.<\/p>\n<h4>Learn more: <a href=\"https:\/\/emizentech.com\/blog\/create-custom-report-types-salesforce.html\" target=\"_blank\" rel=\"noopener\">How To Create A Custom Report Type In Salesforce<\/a> and <a href=\"https:\/\/emizentech.com\/blog\/component-communication-in-lwc.html\" target=\"_blank\" rel=\"noopener\">How To Do Component Communication In LWC<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will learn about fetching records by using LDS. In the previous blog, we learned about how to use Lightning Data Service now let\u2019s get record details by LDS into it. We will use Lightning\/ui*Api Wire Adapters and Functions. Let\u2019s check step by step 1. Import this in your js controller. import<\/p>\n","protected":false},"author":39,"featured_media":3398,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-3384","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"jetpack_featured_media_url":"https:\/\/emizentech.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/Get-Record-Detail-By-LDS-1.jpg","featured_image_src":"https:\/\/emizentech.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/Get-Record-Detail-By-LDS-1.jpg","featured_image_src_square":"https:\/\/emizentech.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/Get-Record-Detail-By-LDS-1.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/emizentech.com\/blog\/author\/salesforce"},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/posts\/3384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/comments?post=3384"}],"version-history":[{"count":1,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/posts\/3384\/revisions"}],"predecessor-version":[{"id":44701,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/posts\/3384\/revisions\/44701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/media\/3398"}],"wp:attachment":[{"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/media?parent=3384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/categories?post=3384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emizentech.com\/blog\/wp-json\/wp\/v2\/tags?post=3384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}