+ Start a Discussion
Kiran Jain 15Kiran Jain 15 

Call Lwc when Record Update From Detail page

hello Peers,
I am stuck in a requirement. My requirement is that when any record is update from detail page then my lwc will  call.
lwc is also on detail page.
If it were in aura, we could use force:recordData but in lwc it not works.

please anybody suggest me what should I do.
it's urgent for me.
Thank in advance
Best Answer chosen by Kiran Jain 15
Surya GSurya G
Hi Kiran,

Here is a sample code, that will show toast event  message when the record is updated in account detail page.
In general, when the page is loaded , it wont fire, but whenever account is edited in detail page, the logic fires the toast event.

we can go with getRecordNotifyChange method, if record changed outside its mechanisms. But I understood, for your requirement, it is not required. we can use 'getRecords' method from uiRecordApi for your need.
 
import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
const FIELDS=['Account.Id'];

export default class ShowToast extends LightningElement {
    @api recordId;
    account;
    lastModifiedDate;
    
    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    wiredRecord({ error, data }) {
        if (error) {
           console.log('error occured');
        } else if (data) {
            this.account = data;
            let modifiedDate = this.account.lastModifiedDate;
            if(!this.lastModifiedDate) {
                this.lastModifiedDate = this.account.lastModifiedDate;
            }
            if (modifiedDate != this.lastModifiedDate) {
                this.showNotification();
            }
        }
    }

    showNotification() {
        const evt = new ShowToastEvent({
            title: 'Success',
            message: 'Account Updated',
            variant: 'success'
        });
        this.dispatchEvent(evt);
    }
}
Let me know if this helps you

Thanks
Surya G

 

All Answers

Surya GSurya G
Hi Kiran, 
we can use ui-record-api in LWC to achieve your requirement.
There is  a method called getRecordNotifyChange, that will help you yo execute the logic only when the record is modified.

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get_record_notify
Thanks
Surya G

 
Kiran Jain 15Kiran Jain 15
hello surya,
Thanks for response, I am using it but I'm not able to capture detail page event. can you give me any example there it's working fine.
it's really urget.
Thank you
 
Surya GSurya G
Hi Kiran,

Here is a sample code, that will show toast event  message when the record is updated in account detail page.
In general, when the page is loaded , it wont fire, but whenever account is edited in detail page, the logic fires the toast event.

we can go with getRecordNotifyChange method, if record changed outside its mechanisms. But I understood, for your requirement, it is not required. we can use 'getRecords' method from uiRecordApi for your need.
 
import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from 'lightning/uiRecordApi';
const FIELDS=['Account.Id'];

export default class ShowToast extends LightningElement {
    @api recordId;
    account;
    lastModifiedDate;
    
    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    wiredRecord({ error, data }) {
        if (error) {
           console.log('error occured');
        } else if (data) {
            this.account = data;
            let modifiedDate = this.account.lastModifiedDate;
            if(!this.lastModifiedDate) {
                this.lastModifiedDate = this.account.lastModifiedDate;
            }
            if (modifiedDate != this.lastModifiedDate) {
                this.showNotification();
            }
        }
    }

    showNotification() {
        const evt = new ShowToastEvent({
            title: 'Success',
            message: 'Account Updated',
            variant: 'success'
        });
        this.dispatchEvent(evt);
    }
}
Let me know if this helps you

Thanks
Surya G

 
This was selected as the best answer
Kiran Jain 15Kiran Jain 15
hy Surya,
Thanks a lot, Really it is helpful for me.You are worthy for best.