How do I display information in a toast message using Apex? I want to show the message via a trigger on the Opportunity object. I can't use addError, because addError prevents the user from saving the record, which isn't what I want.

I've read about ApexPages.addmessage, but that's not working for me. When I try it, it causes the error "There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help."

How do I do this? I'm looking for a solution that works in a trigger on the regular Opportunity object, not in a Lightning component.

My code: ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'My info message'));

1 Answer 1


There's no way to do this without installing some component of yours within the page on the front end, to listen for the message broadcast by your trigger and display it to the user.

You'd have to place a custom Lightning component, or embedded Visualforce page, on the standard layout or in an overridden Lightning Record Page. Then, that component would need to subscribe to one of the several streaming products, most likely a Platform Event. In a Lightning component, you'd do this via <lightning:empApi>.

Your trigger would fire the aforementioned Platform Event based on the specific business logic required, and your listener (on any active user session) would receive it and take the appropriate action, such as showing a Toast message.

You can't do this without doing development work on both the front end and the back end. Standard pages are not listening for events from your trigger to display Toasts.

An alternate approach could be to harness the new Custom Notification feature in Process Builder. Using Custom Notifications could allow you to achieve this end with significantly less code, albeit with less control over the visual presentation to the end user. Note that Custom Notifications aren't Toasts, but do show in the "bell" menu in Lightning.

You must log in to answer this question.

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