function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurabh Kulkarni 84Saurabh Kulkarni 84 

RefreshView event called multiple times for different objects

Hello Everyone

I have a lightning component put on a record page. I'm using RefreshEvent in my component to see if RefreshEvent was done on record page and then perform some action.
<aura:handler event="force:refreshView" action="{!c.handleEvent}"/>

I have my component put on Lead object record page as well as Opportunity record page.

My problem is, when I navigate from Lead Record page to Opportunity record page, and I make some changes on Opportunity record, the RefreshEvent is called twice - one for Lead record (as i had navigated from Lead object, probably it kind of cached it) and one for current Opportunity record. If I'm printing v.recordId in console I clearly see two different record Ids being printed and hence my action is getting called twice for two different records.
 
handleEvent: function(component,event,helper){
        console.log('HANDLER '+component.get('v.recordId'));
}

--
Console Log: 
HANDLER 0063h000004n7r5AAA
HANDLER 00Q3h0000052lCCEAY

How can I tackle this scenario?

Thanks,
Saurabh Kulkarni
Best Answer chosen by Saurabh Kulkarni 84
Saurabh Kulkarni 84Saurabh Kulkarni 84
Hi,

I managed to fix it by checking if the record id variable is same as the one present in the current URL. As refresh event is fired with multiple record Ids, I can check if which one of them matches from current url and then do my stuff.
 
var urlpath = window.location.pathname;
if(urlpath.indexOf(component.get('v.recordId'))>-1){
        //do stuff here..
}

Thanks, 
Saurabh Kulkarni

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Saurabh,

This is just an idea can you try using a boolean to check if the event has already occurred?

Regards,
Anutej
Saurabh Kulkarni 84Saurabh Kulkarni 84
Hi Anutej,

Thank you for your reply.

If I put a flag, my event won't get considered even when I'm performing second valid save on same record. I want the refresh event to call my action every time the record is edited and saved. I can probably verify recordId got from v.recordId variable and record id i have in the URL and then only perform action. However, I don't understand why refresh event from earlier opened record should get fired here.

Thanks 
Saurabh Kulkarni 
sachinarorasfsachinarorasf
Hi Saurabh,

Use bellow js code for getting single record id

<aura:attribute name="refreshTimestamp" type="Integer" default="0" />

forceRefreshViewHandler : function(c, e, h){
    var lastRefresh             = c.get("v.refreshTimestamp")
    if( h.ts() - lastRefresh > 1000 ) {
        // doing my stuff here
        c.set("v.refreshTimestamp" , h.ts() );
    }
}  

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
Saurabh Kulkarni 84Saurabh Kulkarni 84
Hi Sachin,

Thank you for your reply..

Can you please help me out what is h.ts() ? I'm not familiar with any helper method named as ts() or ts abbrevation.

Appreciate your help on this.

Regards,
Saurabh Kulkarni
Saurabh Kulkarni 84Saurabh Kulkarni 84
Hi,

I managed to fix it by checking if the record id variable is same as the one present in the current URL. As refresh event is fired with multiple record Ids, I can check if which one of them matches from current url and then do my stuff.
 
var urlpath = window.location.pathname;
if(urlpath.indexOf(component.get('v.recordId'))>-1){
        //do stuff here..
}

Thanks, 
Saurabh Kulkarni
This was selected as the best answer