I have a Apex Trigger on creation of a record that calls an Apex class. The point of a method in the class is to check the current time of user and see if it is within business hours.

However, I want to display a toast (Either a warning or danger) if the user tries to input time outside of business hours. Is this possible?

Apex Code:

if (userTime > endTime && userTime < startTime)
    System.debug('YOU ARE OUT OF HOURS');
    //This is where I would like to call a toast

I have created toasts within lightning components and javascript controllers, but is it possible to do call the toast here? If so, how?

  • Pretty sure what you are trying to do is not possible. – Adrian Larson Jul 9 '19 at 21:04

The short answer to this is "no, you cannot trigger a toast from Apex". This is because Apex is a server-side language and toasts must be launched from the front-end.

However, as @glls answer says, you can make a call to Apex, interpret its result, and then trigger a toast based on what results are. This is a very common pattern with errors, for example, where the Apex controller throws an exception and then that exception is shown to the user as a toast.

Here is a very brief example:


public class myClass {
    public static String giveMeAMessage(String name) {
        return 'Hello ' + name;

Lightning Aura Component:

callApex : function(component, event, helper) {
    var action = component.get('c.giveMeAMessage'); 

        "name" : 'MYNAME'

    action.setCallback(this, function(a){
        var state = a.getState(); // get the response state
        if(state == 'SUCCESS') {
            helper.showToast(component, event, helper, a.getReturnValue());
        } else {
            helper.showToast(component, event, helper, 'ERROR');

showToast : function(component, event, helper, message) {
    var toastEvent = $A.get("e.force:showToast");
        "title": "Success!",
        "message": "The Controller said: " + message

PS: You probably don't want to put the code above straight into production... there are better ways to make the call to Apex, but I wanted to show a quick example. Trailhead has several trails that will help with that part, including this one, which was also linked to in @glls's answer to this post.

  • “As the other answer posts” :p you make me sound as a stranger – glls Jul 9 '19 at 21:08
  • Hahahaha! I didn't mean to, for sure! I'll make it better – Sebastian Kessel Jul 9 '19 at 21:09
  • There, @glls, I agree this reads a lot better now – Sebastian Kessel Jul 9 '19 at 21:09
  • 1
    No worries, lol. – glls Jul 9 '19 at 21:11
  • So these two communicate by the line "var action = component.get('c.giveMeAMessage'); – Nelson Swasono Jul 9 '19 at 21:50

Normally, toasts are something that you do on the client, yes, you can trigger it with apex, you just need to have the apex class be invoked from the client and return a response that can be handled by your client side controller in order to trigger a toast you will have to use an @AuraEnabled decorator on your apex method and handle the response appropriately.

Connect to Salesforce with Server-Side Controllers should give you a head start on achievieving this functionality

  • Could you go more into detail? So I need my javascript controller call my apex method? – Nelson Swasono Jul 9 '19 at 20:11
  • Thanks for asking Nelson, The trail-head module i linked explains all the details. do you know what an MVC Architecture is? if not, i'd suggest you go through the trailhead to further dig into the basics and how this works. – glls Jul 9 '19 at 20:15
  • basically, the essence of what you are asking would be "how to call Toast FROM an apex class" which involves a number of parties, such as JS (controller) your apex class and your component markup - its not as simple as Apex-> call toast (iirc) – glls Jul 9 '19 at 20:22

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.