0

Running into an issue where RefreshApex isn't working as expected due to a timing issue.

I have an abstracted SOSL query pulling records of two sObject types, and returning whichever was updated most recently. The information from each record is pulled into a wrapper, parsed into JSON, and sent into the LWC. This is working as expected.

Where it goes off the rails is when I submit new records and the two sObjects should be updated, thereby displaying the newly created sObject. This works correctly after a few refreshes of the page, but obviously I would like to prevent the user from having to sit and refresh until the appropriate record appears.

Here is the relevant JS file:

import { LightningElement, api, track, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getJSON from '@salesforce/apex/CLASS.getJSON';

export default class CLASS extends LightningElement {
    @api recordId;
    @track validationRecord;
    _wiredJSON;
    error;
    loaded = false;

    get validationRecordExists() {
        return this.validationRecord != null;
    }


    @wire(getJSON, { recordId: '$recordId' })
    retrieveJSON(value){
        console.log('wire call');
        this._wiredJSON = value;
        const { data, error } = value;   
        if(data){
            console.log("jsonDATA", data)
            this.validationRecord = data;
        } else if(error) {
            console.error(error)
        }
        this.loaded = true;
    }

    handleRefresh() {
        alert('refreshing...');
        this.loaded = false;
        refreshApex(this._wiredJSON)
            .then(() => {
                console.log('refreshsuccesss');
                this.loaded = true;
            })
            .catch((error) => {
                console.log('refreshapexerror', error);
            });
    }
}

I have updated my @wire call based on other similar posts, and in theory it is working correctly per my log statements, but it is not displaying the values as expected. My Apex class is marked as @AuraEnabled(cacheable=true):

public with sharing class CLASS {
    @AuraEnabled(cacheable=true)
    public static String getJSON(String recordId) {
        return CLASS.wrapData(recordId) == null ? null : JSON.serialize(CLASS.wrapData(recordId));
    }
}

Any help is appreciated!

14
  • Are the new record(s) being created by the mentioned component? If you have a custom method for creating records, handleRefresh() should be called when new record is being created.
    – Raul
    Commented Sep 25, 2022 at 23:00
  • @Raul handleRefresh() is being called by a child component creating the new record that is bubbling up to the parent, which is pulling the records. Commented Sep 25, 2022 at 23:01
  • Ah I see, wasn't super clear from the code above. I assume you send a custom event from child to parent, then parent's event handler calls handleRefresh()? Do you see the alert message in that scenario but UI still doesn't refreshes listing newly created record?
    – Raul
    Commented Sep 25, 2022 at 23:04
  • Correct. Event bubbles from child to parent, and then parent's event handler calls handleRefresh(). Alert is showing, logs are showing, newly created record is not showing in the UI. Commented Sep 25, 2022 at 23:05
  • 1
    Darn, that doesn't seem to work either. Thank you for your assistance! Commented Sep 25, 2022 at 23:26

1 Answer 1

0

Was able to figure out a workaround after coming back to this several months later. Played around with it, and refreshApex(this._wiredJSON) wasn't working, as evidenced by lack of console.log statements within the retrieveJSON function, although the refreshsuccess log was showing.

Instead of using refreshApex(), I opted to have the JSON from the created record bubble up to the parent.

You must log in to answer this question.

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