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…
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…
<template> <lightning-card> <lightning-record-form object-api-name={objectApiName} fields={fields} onsuccess={handleSuccess}> </lightning-record-form> </lightning-card> </template>
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); } }
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; }
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; } } }
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 }); } }
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; }
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 }); } }
There was a problem reporting this post.
Please confirm you want to block this member.
You will no longer be able to:
Please note: This action will also remove this member from your connections and send a report to the site admin. Please allow a few minutes for this process to complete.