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!
handleRefresh()
should be called when new record is being created.handleRefresh()
? Do you see the alert message in that scenario but UI still doesn't refreshes listing newly created record?