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
louisa barrett 7louisa barrett 7 

Event being handled even when tab is not open

I have a custom tab that displays a lighning component which on a click event allows a user to edit a record.
I am trying to listen for a save success on the e.force:editRecord and after much searching I found no solution as apparently there is no standard callback for the editRecord
https://trailblazer.salesforce.com/ideaView?id=0873A000000CQQiQAO
As per a few other suggestions I have added an handler for the force:showToast
This is working, albeit a hack, but the problem is as this is an application event it is constantly being listened for even if the custom tab isn't open.
Does anyone have any ideas how I can remedy this
<aura:handler event="force:showToast" action="{!c.handleToastEvent}"/>



handleToastEvent: function(component, event, helper){
        var toastMessageParams = event.getParams();
        var message = toastMessageParams.message;
        if(message.includes('Resource Placeholder') && message.includes('was saved')){
            var calendar = component.get('v._Calendar');
            var calendarEvent = event.getParam("calendarEvent")
            helper.fetchCalendarEvents(component, calendar, calendarEvent);
        }
    }

Many thanks,
David Zhu 🔥David Zhu 🔥
force:showToast is a system event. Once your aura component is instanitiated, it is listening to the event.
I would suggest adding an attribute isTabOpen to check the activate tab, then add <aura:if> to the handler to stop listening to the event if the tab is not open.

<aura:if isTrue="{!v.isTabOpen}">
  <aura:handler event="force:showToast" action="{!c.handleToastEvent}"/>
</aura:if>
louisa barrett 7louisa barrett 7
Hi,

Thank you for the suggestion. I can't wrap the event in an Aura:if though as i get an error of 'invalid attribute "event"
My code is below, I can't spot any syntax mistakes, but apologies if I've missed something
 
<aura:if isTrue="{!v.isTabOpen}">
   <aura:handler event="force:showToast" action="{!c.handleToastEvent}"/>
</aura:if>