2

I have a custom component which goes on a record home page and displays certain related records. The selection logic is somewhat complex, so I cannot use Lightning Data Service; I am calling a custom apex controller. My method to retrieve the data is currently @wired. The component is build as a thin Aura wrapper around an LWC to allow me to raise certain force:* events.

I'm having issues with changes to related records. There are a few scenarios, which probably have the same underlying cause (and hopefully same resolution). Let's say I'm showing certain related contacts on the account page, AND that the standard "Contacts" related list component is also visible. Here are some problem scenarios.

  1. User clicks "New" button on standard Contacts related list and creates a new contact which meets the criteria to show on my custom component. On hitting save, the page navigates to the new Contact. The user then clicks the link back to the account. The standard Contacts related list shows the new contact, but my custom component doesn't show the new record. I'm sure this is because my controller method is @wired and therefore cached. I could call refreshApex if I knew that I needed to. I could also un-wire it and call it imperatively during page load, but this feels un-LWC-like.

  2. User clicks "Delete" button on row in the standard Contacts related list, for a Contact which also appears in my custom component. The system displays a confirmation dialog, and the user confirms the deletion. The system deletes the record and displays a success toast. The standard Contacts related list refreshes to remove the deleted contact from the page, but my component does not. Again, I could call refreshApex if I knew that I needed to. Un-@wire-ing the call to the controller wouldn't help here, as the page isn't being reloaded.

The force:refreshView docs state:

We don’t recommend you to handle this event as it’s handled automatically by the app. To maintain the latest data on your page while reading and modifying your data, we recommend using force:recordData, which is built on Lightning Data Service.

As I stated above, LDS isn't an option in my case. "We don't recommend" implies that it is possible, so I tried adding a handler to my Aura wrapper (<aura:handler event="force:refreshView" action="{!c.handleRefresh}" />) which simply logs to console, but it never fires.

Is there any way to be notified (via Aura- or LWC-available means) of these types of changes? These aren't changes originating from my own component, so I don't know when they occur.

Even if I could eliminate deletes/edits from other components (e.g., removing the standard related list component), I would still have an issue if the user navigates away, makes a change, and navigates back, if my apex controller's response is still cached. I tried to find a way to wire a method but force it to refresh on page navigation (using connectedCallback), but of course any response saved in the component (to use when calling refreshApex) is lost during navigation - so the apex response is cached from a prior page load, but the component has no way of knowing this.

2
  • <aura:handler event="force:refreshView" action="{!c.handleRefresh}" /> should have worked. As the record page does fire a internal refresh event to refresh its view. Can you show your code? As you have eliminated all options, I believe the only left option for you to go for is, Generic Event , which you can use to refresh client view. Jan 2, 2021 at 19:28
  • @NagendraSingh My aura:handler code matches your example exactly. But I'm going to try @PrashantKashyap's answer later when I'm at my desk, and add isUrlAddressable to see if it makes a difference. Jan 3, 2021 at 20:09

1 Answer 1

1

The Documentation for force:refreshView mentions that it refreshes standard components only and Refreshing the following views recreates and refreshes the custom components within it.

  • Action override for a record action using lightning:actionOverride
  • Lightning component that’s directly addressable using lightning:isUrlAddressable
  • Lightning tab

Try making the component UrlAddressable which might trigger the handler.

https://developer.salesforce.com/docs/component-library/bundle/force:refreshView/documentation

4
  • Interesting idea, I will try that later when I'm at my desk. I suspect it may only work when the component is actively being accessed via URL, but it is certainly worth a try. Jan 3, 2021 at 20:11
  • I tested adding lightning:isUrlAddressable to my component; it did not help. On reading the doc page again, I don't think a custom component will ever see this event if it's on a standard record page. Jan 5, 2021 at 2:13
  • I would consider that a roadblock in this case. But this would be such a common use case. Jan 5, 2021 at 4:47
  • If you need a workaround, you might want to use changeevents to let your component know and refresh it that way. Jan 5, 2021 at 4:48

You must log in to answer this question.

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