0

I have created a Lightning Web Component that displays records in a data table. It also contains a child custom modal component which the user can use to create a new record.

I have the save working in the modal, but I'm not sure how to get the data table in the parent component to update (and show the new record).

I was first of all thinking that I would create an event in the child component, passing up the newly created record to the parent, where it would simply be added to the list.

However, that list gets its data via a @wired service, so I was wondering if there is a way to simply trigger this @wired to refresh the data from the server?

0
1

For manually refreshing the wired property,

import { refreshApex } from '@salesforce/apex';
refreshApex(wiredProperty)

For below wired property,

@wire(getOpptyOverAmount, { amount: '$amount' })
opptiesOverAmount;

you have to use below line in any method to refresh it:

return refreshApex(this.opptiesOverAmount);

And for below wired function,

@wire(getActivityHistory, { accountId: '$recordId', max: '500' })
wiredGetActivityHistory(value) {
    // Hold on to the provisioned value so we can refresh it later.
    this.wiredActivities = value;
    ...
}

You need to pass the variable to refreshApex like below:

return refreshApex(this.wiredActivities);

Below is full example

export default class GalleryApexMaster extends LightningElement {
    @api recordId;

    @wire(getActivityHistory, { accountId: '$recordId', max: '500' })
    wiredGetActivityHistory(value) {
        // Hold on to the provisioned value so we can refresh it later.
        this.wiredActivities = value;
        ...
    }

    handleLogACall() {
        // Use the value to refresh wiredGetActivityHistory().
        return refreshApex(this.wiredActivities);
    }
}

Then on button click, or by some event if you invoke handleLogACall(), it will refresh.


Solution in your case:

In your use case, you can dispatch event to parent from child and in parent simple return refreshApex of the wired property

3
  • Thanks for your help again. I agree from your example and the docs that as I am using a wired function, I should be able to pass in the this.wiredActivities property (or the property that the data is being assigned to) to refreshApex() and it should re-render my list, but I'm still getting the same error message for some reason.
    – lemming
    Aug 1 '19 at 15:00
  • If you can show code in question, then I might be able to spot the problem Aug 1 '19 at 15:08
  • Aha. Finally got it to work. The issue was that I assigning result.data to my this.wiredActivities property, rather than result. Thanks again for your help.
    – lemming
    Aug 1 '19 at 15:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.