3

I have a lightning web component that it's data needs to be updated whenever the record is saved.

Is it possible to trigger a function\component refresh when the record is save?

2

2 Answers 2

8

If you are using standard lightning components like record-edit-form or record-form or getRecordUi - All of them use lightning-data-services. So, if the same record is updated in one place, it will be automatically updated in all other places.

However, if you are call Apex Method Imperatively, you can use refreshApex


ADDED

@Dana, as shown in the How to get an LWC component on a record page to refresh when that page is updated, it works fine for record update anywhere (for lightning data service)

11
  • I have the same scenario - the user edits the data via the page for the record and saves the update. I then have an LWC on a tab in the record page and it doesn't get refreshed. The component has nothing to do with the editing of the record, but appears on the same flexipage. It doesn't get updated when the record's details update.
    – Phil W
    Commented Aug 21, 2019 at 10:01
  • @PhilW, I just checked exactly the scenario you told. I used record-edit-form and its updating when updated from record details. Can you pls create a question in detail if what I did is not correct? Commented Aug 21, 2019 at 10:06
  • @salesforce-sas The specific scenario is that i have a progress bar, that it's percentage should be updated once a few fields are not blank. So there is a calculation function that should run every time a record is saved.
    – Dana Griff
    Commented Aug 21, 2019 at 10:10
  • @DanaGriff can you edit the question and give more info and codesnippets? Commented Aug 21, 2019 at 10:11
  • @Pranay jaiswal There is no snippet because i dont know how to trigger a function on record save.
    – Dana Griff
    Commented Aug 21, 2019 at 11:03
2

We can use combination of getRecord and wiredMethod to achieve this. getRecord is called when record is saved & then you can chain a wired method inside it. LastModifiedDate is used to chain these two since it gets updated every time record is saved.

    import { LightningElement, wire, api, track } from 'lwc';
    import { getRecord } from 'lightning/uiRecordApi';
    import someMethod from '@salesforce/apex/SomeCtrl.someMethod';
    export default class SomeCustomComponent extends LightningElement {
        @api recordId;
        @track account;
        lastModifiedDate;
    
        @wire(getRecord, { recordId: '$recordId', fields: [ 'Account.Name', 'Account.Phone' ] })
        getaccountRecord({ data, error }) {
            console.log('accountRecord => ', data, error);
            if (data) {
                this.account = data;
                this.lastModifiedDate = data.lastModifiedDate;
            } else if (error) {
                console.error('ERROR => ', JSON.stringify(error));
            }
        }
        
        error;
        @wire(someMethod, {recordId : '$recordId', lastModifiedDate : '$lastModifiedDate'})
        wiredSomeMethod({ error, data }) {
            if (data) {
                // code goes here
            } else if (error) {
                this.error = error;
            }
        }
    }
1
  • This is bypassing any cache hit by varying the wire parameters. Use of last modified is good because the cache key will still be the same if the record didn't change, but since this is in the context of an update it will always be a cache miss. Be aware that this can proliferate a lot of redundant cache entries in the client and really is a bit of a fiddle than a robust solution. IMHO the right way to work is to avoid use of wire in such a scenario.
    – Phil W
    Commented Dec 10, 2021 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .