+ Start a Discussion
Nellson SwasonoNellson Swasono 

How to call toast in apex code?

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?
NagendraNagendra (Salesforce Developers) 
Hi Nelson,

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 https://trailhead.salesforce.com/en/content/learn/modules/lex_dev_lc_basics/lex_dev_lc_basics_server should give you a head start on achieving this functionality.