0

I've been able to get my RefreshApex method to work correctly - however it appears the issue is that the Refresh occurs before the records are created (which unfortunately has to happen on the Server side due to some complex scenarios).

Is there a straight forward approach I could take to ensure the refresh does not occur until the records are created? I know we could do a full page refresh after a certain time but hoping for a slightly more elegant solution.

Thanks for any and all help.

Code below:

import { LightningElement, api, wire, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import getInvoiceBillingContacts from '@salesforce/apex/InvoiceBillingContactTriggerHelper.getInvoiceBillingContacts';
import importInvoiceBillingContacts from '@salesforce/apex/InvoiceBillingContactTriggerHelper.importInvoiceBillingContacts';
import { refreshApex } from '@salesforce/apex';

const columns = [
    { label: 'IBC Number', fieldName: 'Name', editable: false},
    { label: 'Contact', fieldName: 'Contact_Name__c', type: 'text', editable: false },
    { label: 'Contract', fieldName: 'Contract_Name__c', type: 'text', editable: false },
    { label: 'Invoice Email', fieldName: 'Invoice_Email__c', type: 'email', editable: false },
    { label: 'Enabled for this Contract', fieldName: 'Enabled_for_this_Contract__c', type: 'boolean', editable: true },
];

export default class InvoiceBillingContactList extends LightningElement {

    @api recordId;
    invoiceBillingContacts = [];
    error;

    columns = columns;
    rowOffset = 0;

    _wiredResult;

    @wire(getInvoiceBillingContacts, {contractId: '$recordId'})
    wiredIBC({ error, data }) {
        if (data) {
            this._wiredResult = data;
            this.invoiceBillingContacts = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.invoiceBillingContacts = undefined;
            console.log('error' + error);
        }
    }

    importContact(){

        console.log('entered logic @2');
        console.log('recordId: ' + this.recordId);

        importInvoiceBillingContacts({recordId: this.recordId})
        .then(() => {
            console.log('refreshing Contacts $$');
            return refreshApex(this._wiredResult);
        })
        .catch((error) => {
            this.message = 'Error received: code' + error.errorCode + ', ' +
                'message ' + error.body.message;
            console.log(this.message);
        });

    }

}
2

This is resolved in the Winter 21 release. A new method allows you to notify the runtime that a record has definitely changed and needs to be refreshed:

import { LightningElement, wire } from 'lwc';
import { getRecord, getRecordNotifyChange } from 'lightning/uiRecordApi';
import apexUpdateRecord from '@salesforce/apex/Controller.apexUpdateRecord';
 
export default class NotifyRecordChangeExample extends LightningElement {
    @api recordId;
 
    // Wire a record.
    @wire(getRecord, { recordId: '$recordId', fields: ... })
    record;
 
    async handler() {
      // Update the record via Apex.
      await apexUpdateRecord(this.recordId);
      // Notify LDS that you've changed the record outside its mechanisms.
      getRecordNotifyChange([{recordId: this.recordId}]);
    }
}

For now, the alternative is to actually call refreshApex using the wired result (not just the data attribute):

@wire(getInvoiceBillingContacts, {contractId: '$recordId'})
wiredIBC(result) {
    this._wiredResult = result;
    let { data, error } = result;
    if (data) {
        this.invoiceBillingContacts = data;
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.invoiceBillingContacts = undefined;
    }
}

Note that the reason why your original method didn't work is because you only stored the data attribute. You need to store the entire wire result in order to use refreshApex.

| improve this answer | |
  • As always you are a prince among men - thank you! – cg13 Sep 18 at 16:56

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.