Lightning Web Components and Salesforce Data
In this blog, we create an input record form and call the apex class in the Lightning web component. In record form, we have used NAME_FIELD, REVENUE_FIELD, INDUSTRY_FIELD fields of the Account object.
accountCreator.html
We created the HTML form and bind the data with help of object-api-name and fields which get the value from the .js file.
<template> <lightning-card> <lightning-record-form object-api-name={objectApiName} fields={fields} onsuccess={handleSuccess}> </lightning-record-form> </lightning-card> </template>
accountCreator.js
In .js file, we import the object and Fields API name and pass it to the HTML variables. The toast message event is assigned there when records are saved successfully in our Salesforce org.
import { LightningElement } from 'lwc'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import ACCOUNT_OBJECT from '@salesforce/schema/Account'; import NAME_FIELD from '@salesforce/schema/Account.Name'; import REVENUE_FIELD from '@salesforce/schema/Account.AnnualRevenue'; import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry'; export default class AccountCreator extends LightningElement { objectApiName = ACCOUNT_OBJECT; fields = [NAME_FIELD, REVENUE_FIELD, INDUSTRY_FIELD]; handleSuccess(event) { const toastEvent = new ShowToastEvent({ title: "Account created", message: "Record ID: " + event.detail.id, variant: "success" }); this.dispatchEvent(toastEvent); } }
Don't forget to check out: Salesforce Lightning Component Inside Lightning Web Component Using Visualforce Page
wireGetRecordProperty.js
import { LightningElement, api, wire } from 'lwc'; import { getRecord } from 'lightning/uiRecordApi'; import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name'; export default class WireGetRecordProperty extends LightningElement { @api recordId; @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_NAME_FIELD] }) account; }
wireGetRecordFunction.js
import { LightningElement, api, wire } from 'lwc'; import { getRecord } from 'lightning/uiRecordApi'; import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name'; export default class WireGetRecord extends LightningElement { @api recordId; data; error; @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_NAME_FIELD] }) wiredAccount({data, error}) { console.log('Execute logic each time a new value is provisioned'); if (data) { this.data = data; this.error = undefined; } else if (error) { this.error = error; this.data = undefined; } } }
ldsCreateRecord.js
import { LightningElement} from 'lwc'; import { createRecord } from 'lightning/uiRecordApi'; import ACCOUNT_OBJECT from '@salesforce/schema/Account'; import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name'; export default class LdsCreateRecord extends LightningElement { handleButtonClick() { const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields: { [ACCOUNT_NAME_FIELD.fieldApiName] : 'ACME' } }; createRecord(recordInput) .then(account => { // code to execute if create operation is successful }) .catch(error => { // code to execute if create operation is not successful }); } }
Use Apex Methods with Lightning Web Components
There are two ways to interact with Apex methods in LWC which are given below one by one:
- Call Apex Using @wire
- wireApexProperty.js
import { LightningElement, api, wire } from 'lwc'; import getRelatedContacts from '@salesforce/apex/AccountController.getRelatedContacts'; export default class WireApexProperty extends LightningElement { @api recordId; @wire(getRelatedContacts, { accountId: '$recordId' }) contacts; }
Check out another amazing blog by Aditya here: Mail a Table of Opportunity Records With a Link of Detail Page | Salesforce Tutorial
callApexImperative.js
import { LightningElement, api, wire } from 'lwc'; import getRelatedContacts from '@salesforce/apex/AccountController.getRelatedContacts'; export default class CallApexImperative extends LightningElement { @api recordId; handleButtonClick() { getRelatedContacts({ //imperative Apex call accountId: '$recordId' }) .then(contacts => { //code to execute if related contacts are returned successfully }) .catch(error => { //code to execute if related contacts are not returned successfully }); } }
Responses