Lightning Data Service in LWC
September 20, 2023
Lightning Data Service (LDS)
- Standard controller of LWC.
- FLS(Field Level Security) embed by default.
- Respect the security and sharing settings.
- No need to write Server side controller to make CRUD.
- LDS returns data and metadata at the same time.
- It cache data if connection interrupted it will sync the data when connection is back.
- lightning-record-edit-form, and lightning-record-view-form, lightning-record-form.
1- Lightning Record Form
recordFormLWC.html
<template>
<lightning-card title="Contact">
<lightning-record-form object-api-name={objectApiName} record-id={recordId} fields={fields}>
</lightning-record-form>
</lightning-card>
</template>
recordFormLWC.js
import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
export default class RecordFormLWC extends LightningElement {
fields = [NAME_FIELD,PHONE_FIELD];
@api recordId;
@api objectApiName;
}
2- Lightning Record Edit Form
recordEditFormLWC.html
<template>
<lightning-card title="Contact">
<lightning-record-edit-form object-api-name={objectApiName} record-id={recordId}>
<lightning-input-field field-name={nameField}> </lightning-input-field>
<lightning-input-field field-name={phoneField}> </lightning-input-field>
<div class="slds-align_absolute-center">
<lightning-button variant="brand" type="submit" label="Save">
</lightning-button>
</div>
</lightning-record-edit-form>
</lightning-card>
</template>
recordEditFormLWC.js
import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
export default class RecordEditFormLWC extends LightningElement {
nameField = NAME_FIELD;
phoneField = PHONE_FIELD;
@api recordId;
@api objectApiName;
}
3- Record View Form
recordViewFormLWC.html
<template>
<lightning-card title="Contact">
<lightning-record-view-form object-api-name={objectApiName} record-id={recordId}>
<lightning-output-field field-name={nameField}> </lightning-output-field>
<lightning-output-field field-name={phoneField}> </lightning-output-field>
</lightning-record-view-form>
</lightning-card>
</template>
recordViewFormLWC.js
import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
export default class LightningRecordViewFormDemo extends LightningElement {
nameField = NAME_FIELD;
phoneField = PHONE_FIELD;
@api recordId;
@api objectApiName;
}
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
1
1
3
9
Users Today : 0
Users Yesterday : 0
Users Last 7 days : 11
Users Last 30 days : 65
Users This Month : 0
Users This Year : 226
Total Users : 1139
Views Today :
Views Yesterday :
Views Last 7 days : 13
Views Last 30 days : 107
Views This Month :
Views This Year : 347
Total views : 1934
Who's Online : 0
Your IP Address : 216.73.216.50
Server Time : May 2, 2026 10:04 amPowered By WPS Visitor Counter