18

I have a custom Lightning Component sitting on a Flexipage for e.g. Account. Now I want to register an handler, which will be invoked each time, the record is updated. Updates might happen via

  • standard Record Detail component on the same Flexipage
  • other custom components on the same Flexipage
  • triggers

What's the best way to detect and handle as many of these updates as possible?

I've tried so far:

<aura:handler name="onUpdated" event="force:recordUpdated"action="{!c.events}" />
<aura:handler name="onSaveSuccess" event="force:recordSaveSuccess" action="{!c.events}"/>

with

events : function(cmp, evt, hlp){
    console.log('EVENTS :: ',evt.getName(),evt.getParams());
},

but nothing in the console so far. Any ideas?

5

You could use Lightning Data Service to handle the record changes, but its still in Developer Preview which means that you won't be able to use it in a Production or Sandbox org.

Next option is to use Streaming API, to listen for record updates you can find it here. Still I feel it's hacky way.

Final option though I don't like either,

1) Regularly poll the server for the record using window.setInterval().

2) Compare with local version of the record in the client.

3) If record has changed, refresh data in the component.

  • 1
    setInterval is the best method we've found so far. Updates on any one specific record are pretty rare, so we just poll the server every 30 seconds. Far as I can tell, we're not pinged for API usage with this technique (at least, for now), so it works out well. – sfdcfox Jan 12 '17 at 6:04
  • @sfdcfox I've added a new answer on force:refreshView which could possibly reduce the need of setInterval(). Have you ever tried that and found use-cases where it was insufficient? Would be interested what aspects are not coverd by force:refreshView to be aware of in my implementations. – Uwe Heim May 11 '18 at 8:14
5

Actually I'm using the event force:refreshView which is doing what I want:

<aura:handler event="force:refreshView" action="{!c.forceRefreshViewHandler}"/>

and in the controller

forceRefreshViewHandler : function(cmp, evt, hlp){
    // your logic here
},

If my stuff is making an update, which the flexipage should update on, I'm firing the very same event

$A.get("e.force:refreshView").fire();

Tricky part is, if two components are updating each others: then you have to set a flag or something to prevent endless loops.

Lots of Salesforce Standard components are firing and listening for this event, so it's pretty useful.

Caveats

  • updates made by triggers or API integrations probably get NOT detected
  • updates made by components not firing force:refreshView are not detected
  • I just tried this and I couldn't make it to work. Basically, I have a component that sits on Account's Flexipage and listens for this force:refreshView event, and it never catches it. Even though I see it being fired in the Lightning Inspector on chrome. BTW, I'm trying to catch some event that will let me know that the standard Quick Action has been saved/closed. – smukov Nov 12 '18 at 14:47
0

I'd take a look at this. Seems to do the job adequately for me on record pages. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_refreshView.htm

Doesn't explicitly refresh a specific component but it does refresh the page's components when you update the underlying record via your own code / controller.

0

You can try PushTopic for that. All you have to do is create a push Topic in lightning Components. Not best of Solution But can work. https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/working_with_pushtopics.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.