I want to perform an action when a notification happens in my app. The toast event is raised by SFDC which means I do not have access to the calling code.

I have tried adding a handler to the force:showToast event with no luck:

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

There is also this new notificationsLibrary which has the ability to show toasts, but I can't figure out how to tap into it.

  • If the component runs inside LEX then firing the showToast event will definitely invoke the doAction method. Can you please confirm again? – Praveen Feb 11 '18 at 10:47
  • @Praveen As mentioned I am not firing the event; I am trying to listen for system toast notifications (e.g. when a record is created). – Mossi Feb 11 '18 at 10:50
  • Could please add more context like where exactly you are trying to listen for the event? if you share context in bit detail then we can help you better. – Praveen Feb 11 '18 at 10:54
  • This sounds like an x-y problem. If you want to listen for when a record is saved, force:recordSaveSuccess should do the trick. – sfdcfox Feb 11 '18 at 17:43
  • @sfdcfox That's possible! But isn't force:recordSaveSuccess fired only when there's a force:recordEdit? I am not using it in my component. Basically I am trying to capture the outcome of force:createRecord without polling (I have found a way to prevent it from auto-redirecting after success -which is why the framework doesn't define any callback or event for it). I thought the toast notification was a good enough alternative. But as well I have always wanted to know whether I can listen for toast messages. Hence the generic question. – Mossi Feb 11 '18 at 18:24

Yes, you can get the toast information, as it is an application event, and thus propagates to all interested listeners.

Here's the code I wrote that demonstrates this:

<aura:component implements="flexipage:availableForAllPageTypes">
    Toast Processor
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <aura:handler event="force:showToast" action="{!c.toastInfo}" />

    init: function(component, event, helper) {
        var toast = $A.get("e.force:createRecord");
            entityApiName: "Account"
    toastInfo: function(component, event, helper) {
        alert("Toast detected: "+event.getParams().message);

Go to the Lightning App Builder, add this component to a new app, activate it, and then pull it up in Lightning.

After creating the new account, you'll get an alert box saying something like "Toast detected: Client "Test" was created." (Note, my Accounts are renamed as Clients).

| improve this answer | |
  • 1
    What if the toast message is generated by another component? Does your handler still get called? If it matters, I have 3 components: the parent cmp encompasses the child cmp, which dynamically calls up a 3rd cmp which fires the force:createRecord event. I am trying to detect the toast event in the middle cmp (child cmp). – Mossi Feb 11 '18 at 23:00

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.