Before Starting the code, let understand some basic concepts:-
Lightning Web Components has three decorators that add functionality to a property or function.
The ability to create decorators is part of ECMAScript, but these three decorators are unique to Lightning Web Components.
How to create the record in Lightning Web Component without using Apex.
Apex is no longer necessary to create the record in Salesforce Database because of the alternatives for working with data in Lightning Web Component.
You can create the record in LWC by using:-
Let’s see how you can create the record by using “lightning-record-form”
Here, In object-api-name, you pass objectName and in fields,you pass objectFields.
Here, we import Account objectName and three AccountFields(Name, Website, Industry).
Now, you can create the record using JavaScript API Method “createRecord”
Here, we set two actions “onNameChange” at lightning-input and “createAccount” at lightning-button click to create the record.
Here, we import Account objectName and one AccountField(Name). And also Import {createRecord} from ‘lightning/uiRecrodApi’.
Use “@track” to track a private property’s value and re-render a component when it changes.
At “onNameChange” we set the lightning-input value. And at “createAccount”, we set the objectApiName and objectFields in recordInput Object.
After that, pass the recordInput Object to createRecord.If the record is created, “.then” is performed and we set accountId is equal to account.Id.If the record is not created, then we get the error.
How to fetch, update and delete the records in Lightning Web Components?
Step 1) First, we create the Lightning Web Component with name “fetchUpdateDeleteRecord”, click this link on how to create Lightning Web Component. And Create ApexController with name “AccountController”
Here we add three actions, “onNameChange” at lightning-input, “updateAccount” at lightning-button-icon to update the record and “deleteAccount” at lightning-button-icon to delete the record.
You can also see that we used key= objectId. It is necessary to display the record at DOM.
Here, we import the { updateRecord },{ deleteRecord } to perform the update and delete operation. So, you do not need Apex in LWC to perform the create, update and delete operation.
import {refreshApex} to perform the refresh operation at apex queries. And also import ‘getAccountRecordWithRelatedContactList‘ method from AccountController class to fetch the record.
Here is the code of
Use “@track” to track a private property’s value and re-render a component when it changes.
Fetch the record
Use “@wire” service to fetch the record from Salesforce Data.
1. Do not use “@wire” service to create, update and delete data. Pass getAccountList in “@wire” to fetch the record. Here, wiredContacts is just a name. You give any name.
Update the record
Next, we perform updateAccount event to update the record.
First, we get the record Id which we set at data-
Then recordInput pass into updateRecord to perform the update operation. If the record is updated, “.then” promise function will be performed else we will get the error.
Next, we perform deleteAccount event to delete the record.
First, we get the record Id which we set at data-recordid in lightning-button. Then recordId pass into deleteRecord to perform delete operation.If the record is deleted, “.then” promise function will be performed else we will get the error.
Related Blogs:
Quick Start: Lightning Web Components
How to interact one Lightning Web Component with Aura Component
Resources:
Salesforce Developer Guide: lightning/uiRecordApi
Lightning Web Components for Aura Developers: Work with Salesforce Data
4 comments on “How to use decorators( wire, track, api) and work with Salesforce Data in Lightning Web Components”
Hi Nijam,
Can you please help , how update the Status if a Case to Close using Lightning Web Component through a Button.
Thanks,
Kiran
Hi Nigam,
I got this error when I am deploying to org “No MODULE named markup://c:IdsUtils” . Can you post your IdsUtils LWC
thanks for post this blog it really helped me.
Awesome Article.It helped me to a lot.Thank you for your good work.
ldsUtils Class is missed
Comments are closed.