+ Start a Discussion
test vijaytest vijay 

I want old value and new value using getRecord in lwc

Hello Developers.
I have a Lwc Component that is on recordPage.I want to old value and new value that is changed when Record is update.
Can anyone suggest me how to achieve it.
Thanks in advance
Surya GSurya G
Hi Vijay,
Here is the sample code, that get old and new values of account object and display it in a account detail page.
whenever the account record is updated, this component shows the old values and the updated new  values for name , phone and email field

HTML:
<template>
    <lightning-card title="Account Update">

    
    <div class="slds-grid slds-gutters slds-align_absolute-center">
        <div class="slds-col ">
          <span>
            <template if:true={oldAccount}>
            <h1>Old Name  :   {oldAccountName}</h1>
            <h1>Old Phone :   {oldAccountPhone}</h1>
            <h1>Old Email :   {oldAccountEmail}</h1>
            </template>
          </span>
        </div>
        <div class="slds-col">
          <span>
            <template if:true={newAccount}>
                <h1>New Name  :   {newAccountName}</h1>
                <h1>New Phone :   {newAccountPhone}</h1>
                <h1>New Email :   {newAccountEmail}</h1>
            </template>
          </span>
        </div>
      </div>
   
    
</lightning-card>
</template>


JS:
import { LightningElement, api, wire, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class AccountLwc extends LightningElement {
    @api recordId;
    @track newAccount;
    @track oldAccount;
    @track account = [];
    newName;
    name;

    @wire(getRecord, { recordId: '$recordId', fields: [ 'Account.Name', 'Account.Phone' , 'Account.Email__c'] })
    getaccountRecord({ data, error }) {
        console.log('accountRecord => ', data, error);
        if (data) {
            this.processRelatedObjects(data);
        } else if (error) {
            console.error('ERROR => ', JSON.stringify(error)); // handle error properly
        }
    }

    processRelatedObjects(data) {
        console.log('processRelatedObjects for => ', JSON.stringify(data));
        // further processing like refreshApex or calling another wire service
        this.account.push(data);
        if(this.account.length >2) {
            this.account.shift();
        }
        this.oldAccount = this.account[0] ? this.account[0] : {};
        this.name = this.oldAccount.fields.Name.value;
        if(this.account[1]) {
            this.newAccount = this.account[1];
            this.newName = this.newAccount.fields.Name.value;
        }
        
    }
    get oldAccountName(){
        return this.oldAccount.fields.Name.value ? this.oldAccount.fields.Name.value : '';
    }
    get newAccountName(){
        return this.newAccount.fields.Name.value ? this.newAccount.fields.Name.value : '';
    }
    get oldAccountPhone(){
        return this.oldAccount.fields.Phone.value ? this.oldAccount.fields.Phone.value : '';
    }
    get oldAccountEmail(){
        return this.oldAccount.fields.Email__c.value ? this.oldAccount.fields.Email__c.value : '';
    }
    get newAccountPhone(){
        return this.newAccount.fields.Phone.value ? this.newAccount.fields.Phone.value : '';
    }
    get newAccountEmail(){
        return this.newAccount.fields.Email__c.value ? 
         this.newAccount.fields.Email__c.value : '';
    }

    }


Remember, the old values are stored temporarily in JS class, and it will be removed if the component is rerender on the page.
To have permanent old values, try creating a child object that has record with old values.

Thanks
Surya G