0

I have an LWC that has a list of records from a custom object. I inserted this LWC on my standard Account page layout. I then created a platform event and on my trigger for the custom object I publish the event.

The event is getting back to my LWC and I see the handleEvent method that I created gets fired when the platform event fires.

My question is around getting my wire to refresh. The wire service calls an apex query that returns a list of records from the custom object. From my LWC I am using the standard page edit to edit and save a record. I am trying to get the list to refresh.

I'm struggling on how to get refreshApex to have the wire service refresh my list.

Here is the handleEvent on my lwc that is getting called after I save the edited record:

handleEvent = event => {
  const refreshRecordEvent = event.data.payload;
  console.log('$$!! 1 A im in the handleEventand here is the event record id ' + refreshRecordEvent.Record_Id__c);
  console.log('$$!! 1 B im in the handleEventand here is the this.recordid ' + this.recordId);
  ///if (refreshRecordEvent.Record_Id__c === this.recordId) {
      //this.recordId = '';
      this.TestMethod();
      return refreshApex(this.data);
  ///}
}

Here is my wire service:

@wire(getSifs, {accountId: '$recordId'})
//@wire(getSifs, {accountId: this.recordId})
wiredRecords({ error, data }) {
  refreshApex(this.sifs);
  this.refreshTable = data;
   if (data) {
     //set variable indicating we have fired this wire
     this.data = data;
     this.sifs = data;
     data.forEach(rec => {
      if(rec.Effective_Date__c > today){
        this.futureSifs.push(rec); 
      }
      else if(rec.Record_Active_Today__c == 'Active'){
        this.activeSifs.push(rec);
      }
      else{
        this.inactiveSifs.push(rec); 
      }
    });
   } 
   else if (error) {
       this.error = error;
       this.sifs = undefined;
       this.data = undefined;
   }
} 

1 Answer 1

2

Tt's not entirely obvious from the documentation, but the wire object actually has at least three properties. What makes the wire method work with refreshApex is the property you don't get if you use { data, error }. You need to call refreshApex on the entire response object, not just on the data property.

wiredRecords(response) {
  const { data, error } = response;
  this.sifs = response;
  // rest of logic here
}

Don't call refreshApex from within the wiredRecords handler. This potentially results in an infinite loop.

Your ESB handler can call refreshApex:

handleEvent = event => {
  const refreshRecordEvent = event.data.payload;
  if (refreshRecordEvent.Record_Id__c === this.recordId) {
    return refreshApex(this.sifs);
  }
}

From there, the wire method will be refreshed.

1
  • Awesome! Thanks for the explanation, makes total sense now!!
    – Frank
    Commented Apr 8, 2022 at 12:35

You must log in to answer this question.

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