Play Games

Search This Blog

Thursday, January 6, 2022

How do update a salesforce record from Lightning Web Component without using Apex - Salesforce Globe For You

How do update a salesforce record from Lightning Web Component without using Apex - Salesforce Globe For You

Solution: Import the updateRecord from 'lightning/uiRecordApi' to update a record.

Example: Assume lead id is '00Q0o00001ngIlLEAU' and lead last name is 'UpdateRecord'

updateRecordWithoutApexInLWC.html

<template>

    <lightning-card title="Update Lead Record Without Apex" icon-name="standard:record">

        <div class="slds-m-around_small">

                Lead Id:{leadId} 

        </div>

        <div class="slds-m-around_small">

                Initial Lead Last Name:{name} 

        </div>

        <div class="slds-m-around_medium">

        <lightning-button label="Update Lead" variant="brand" onclick={updateLead}></lightning-button>

    </div>

    </lightning-card>

</template>

updateRecordWithoutApexInLWC.js

import { updateRecord } from 'lightning/uiRecordApi';

import { LightningElement, api } from 'lwc';

import LASTNAME_FIELD from '@salesforce/schema/Lead.LastName';

import ID_FIELD from '@salesforce/schema/Lead.Id';

import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import { NavigationMixin } from 'lightning/navigation';

export default class UpdateRecordWithoutApexInLWC extends NavigationMixin(LightningElement) {

    @api leadId ='00Q0o00001ngIlLEAU';

    @api name ='UpdateRecord';

    updateLead() {

        const fields = {};

        fields[ID_FIELD.fieldApiName] = this.leadId;

        fields[LASTNAME_FIELD.fieldApiName] = 'Name Updated';

        const recordInput = { fields };

        updateRecord(recordInput)

            .then(() => {

                this.dispatchEvent(

                    new ShowToastEvent({

                        title: 'Success',

                        message: 'Lead Last Name Updated Successfully',

                        variant: 'success'

                    })

                );

                this[NavigationMixin.Navigate]({

                    type: 'standard__recordPage',

                    attributes: {

                        recordId: this.leadId,

                        objectApiName: 'Lead',

                        actionName: 'view'

                    }

                });

            })

            .catch(error => {

                console.log('Error',error);

                this.dispatchEvent(

                    new ShowToastEvent({

                        title: 'Error Updating record',

                        message: error.body.message,

                        variant: 'error'

                    })

                );

            });

    }

}

updateRecordWithoutApexInLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>48.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



No comments:

Post a Comment