1

I have facilitated a button to create a record through a button with help of navigation Mixin. I need to refresh my data table with a record that has been created. I am trying to refresh the data, but the table is not getting refreshed.

datatable.html

<template>
    <lightning-button
        label="Looks Fine"
        onclick={handleSelectedFields}
    >
    </lightning-button>
    <lightning-button
        variant="brand"
        label="Add Row"
        title="Primary action"
        onclick={handleAddRow}
        class="slds-m-left_x-small"
    >
    </lightning-button>
    <lightning-datatable
        key-field="id"
        data={fetchedContacts}
        columns={columns}
    >
    </lightning-datatable>
</template>

datatable.js

import { LightningElement, track, wire,api } from 'lwc';
import getRecords from '@salesforce/apex/ExploreCustomContactController.getRecords';
import getContactFields from '@salesforce/apex/ExploreCustomContactController.getContactFields';
import getFieldTypes from '@salesforce/apex/ExploreCustomContactController.getFieldTypes';
import { NavigationMixin } from 'lightning/navigation';
import { refreshApex } from '@salesforce/apex';

export default class ExploreReimplementDataTable extends NavigationMixin(LightningElement) {
    @track columns = [
        { label: 'Id', fieldName: 'Id' },
        { label: 'Name', fieldName: 'Name' }
    ];
    @track selected = ['Id','Name'];
    @track fetchedContacts = [];
    @api objectApiName;
    refresh;
    @wire(getContactFields , { objectApiName : '$objectApiName'}) wiredContactFields;
    @wire(getRecords, { selectedFields: '$selected' , objectApiName: '$objectApiName'})
    wiredContacts(result) {
        this.refresh = result;
        if (result.data) {
            this.fetchedContacts = result.data;
            console.log(this.fetchedContacts);
        } else if (result.error) {
            console.log(result.error);
        }
    }
    handleAddRow(){
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: this.objectApiName,
                actionName: 'new'
            }
        });
        refreshApex(this.refresh);
    }
}

I am not knowing how to refresh the table after creating record.

1
  • It looks like you are navigating to the new record popup - but at that point you won't have created the record. You'd want to refresh the table on close of the new record popup. This could be difficult to detect as it's a built in component. Perhaps you could use a flow and despatch a platform event, and listen for it using the empApi, and refresh based on that? Commented Mar 14, 2022 at 18:25

2 Answers 2

1

Try calling the refresh method in then method of navigation service as it returns a promise:

this[NavigationMixin.Navigate]({
     type: 'standard__objectPage',
     attributes: {
         objectApiName: this.objectApiName,
         actionName: 'new'
     }
}).then(() => {
     refreshApex(this.refresh);
});
2
  • Hi Raul, this seems only to work for NavigationMixin.GenerateUrl. "Navigate" does return undefined instead of a promise.
    – itsmebasti
    Commented Jun 14, 2021 at 13:18
  • Maybe need to use bind method to pass this context or store this reference as self and use it for redirect.
    – Raul
    Commented Jun 14, 2021 at 13:28
0

refresh could be part of predefined keyword. I tried with changed name and it worked for me.

  _wiredRefreshData;

@wire(getRecords, { selectedFields: '$selected' , objectApiName: '$objectApiName'})
wiredContacts(result) {
    this._wiredRefreshData = result;
    if (result.data) {
        this.fetchedContacts = result.data;
        console.log(this.fetchedContacts);
    } else if (result.error) {
        console.log(result.error);
    }
}
handleAddRow(){
    this[NavigationMixin.Navigate]({
        type: 'standard__objectPage',
        attributes: {
            objectApiName: this.objectApiName,
            actionName: 'new'
        }
    });
    refreshApex(this._wiredRefreshData);
}

I hope this will help you Ref : https://salesforce.stackexchange.com/a/336820/29547

You must log in to answer this question.

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