0

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.

0

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);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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