I have a parent LWC component componentP where I have used Tabset and under that I have different Tabs.
For TabA I have a componentA from where I am updating/ inserting the records in ObjectA, and for updating I am using a apex controllerA. Now, once the insertion is done, when I switch to TabB I don't see the refreshed / latest data entered in my table. For TabB I am using componentB , and I am using apex componentB to fetch those records.
So, I want to know, once I save the record from TabA why it's not showing the latest details in TabB. How can I do that? I have tried refreshApex, using @wire
and AuraEnabled(cacheable=true)
to fetch the records, but it's not working.
Here's the code for your reference. When The page loaded it should display the presernt data in object, also when any new record is inserted it should automatically show the data without page load.
1.a. paremt componentP HTML
<template>
<lightning-tabset variant="Scoped">
<lightning-tab label="TabA" onactive={handleActive}>
<c-component-a saved-stories={savedStories} tab-label={tabLabel} onsavedrecord={confirmation}></c-component-a>
</lightning-tab>
<lightning-tab label="TabB" onactive={handleActive}>
<c-component-b saved-stories={savedStories} got-confirmation={gotConfirmation}></c-component-b>
</lightning-tab>
</lightning-tabset>
</template>
1.b Parent JS:
{
handleActive(event){
this.tabLabel = event.target.label;
let component;
switch(this.tabLabel){
case 'TabA':
component = this.template.querySelector('c-component-a');
break;
case 'TabB':
component = this.template.querySelector('c-component-b');
break;
}
setTimeout(()=>{
component.refreshData();
},0);
}
gotConfirmation;
confirmation(event){
this.gotConfirmation = event.detail;
}
}
- LWC componentA
import { LightningElement,api,track} from 'lwc';
import saveRecordOnClick from '@salesforce/apex/SDLC_DesignController.saveRecordOnClick';
export default class ComponentA extends LightningElement {
isSaved=false;
saveComponentSpec(){
this.isSaved =true;
console.log('Input values --> '+this.selectedRowIds +' --- '+this.formattedText);
saveRecordOnClick({userStoryIds:this.selectedRowIds,formattedResponse:this.formattedText})
.then(result=>{
const evt = new ShowToastEvent({
title: 'Success',
message: 'Component Specification is Saved',
variant: 'success'
});
this.dispatchEvent(evt);
this.dispatchEvent(new CustomEvent("savedrecord",{detail: this.isSaved}));
})
.catch(error=>{
console.log('ERROR -->'+error.message);
});
this.isSaved=false;
}
}
- LWC componentB
import fetchData from '@salesforce/apex/apexControllerA.fetchData';
import { refreshApex } from '@salesforce/apex';
export default class ComponentB extends LightningElement {
@wire gotConfirmation
@wire(fetchData)
wiredData({result}){
this._wiredResults = result;
if(result.data){
let formattedData = [];
data.forEach(record =>{
formattedData.push({
id: Math.random(),
name:record.name,
});});
this.data = formattedData;
if(this.gotConfirmation){
refreshApex(this.data);
}
}else if(result.error){
console.log('error==>'+error);
}
}
}
- controller for fetching the data
public with sharing class apexControllerA {
@AuraEnabled(cacheable=true)
public static List<Map<String, Object>> fetchData(){
List<objectA__c> userStoryList = [SELECT Id,name, Title__c FROM objectA__c Order By CreatedDate Desc];
//logic
List<Map<String, Object>> data = new List<Map<String, Object>>();
//some logic
return data;
}
}