Toast Notifications in LWC with example

Toast Notifications in LWC with example

In this blog, we will discuss the Toast Notifications in LWC with an example. LWC, which stands for Lightning Web Components, provides a modern way to build UI components in Salesforce. One of the useful features that LWC offers to improve user experience is the Toast Notifications.

Toast notifications are small pop-up messages that appear at the top of the screen for a few seconds and then disappear automatically. In this way, they help by giving instant feedback to users, such as showing success messages, error alerts, or important updates.

Additionally, Salesforce provides the ShowToastEvent in LWC, which is part of the lightning/platformShowToastEvent module, to display these messages effectively.

Let’s see how can we implement Toast Notification in LWC

To execute Toast Notification we have to first import it first from its module “lightning/platformShowToastEvent”. Below is the syntax of how can we import ShowToastEvent

 

After importing we have to create and dispatch the event with title, message, variant, and mode properties.

 

Toast Notification in LWC

Here we have created a function showToastNotification in which we have created an event of type ShowToastEvent with name showToastEv with its attributes (title and message) and then we have dispatched that event with this.dispatchEvent.

Different types of  Toast Notification in LWC

Toast Notification has 4 different types of variants. The following are the types:

  1. Success – Displays a success message.
  2. Error – Shows an error message.
  3. Info – Displays a warning message.
  4. Warning – Provides additional information.

Let’s take an example of a basic toast notification. Here we will be creating a button which displays the notification whenthe  user clicks on it. To create the LWC component in VS Code we have two options:

  1. Right-clicking on the lwc folder and then creating the lightning web component
  2. Another way is Cmd/Ctrl +Shift + P and then open the SFDX command to create a Lightning web component.

 

First way of Creating component

 

Another way of creating component

So, we have created a lwc component DemoToastNotification to show the example. We will be deploying the component on a Lightning App Page. Below is the syntax for displaying the notification.

demoToastNotification.html

 

demoToastNotification.js

 

demoToastNotification.js-meta.xml

 

OUTPUT

 

output 2

 

Output 2

 

Let’s understand each variant with an example.

Success

In this scenario, we are creating an account record with the use of the lightning record form. We are displaying a Toast Notification when the user clicks on the Save button. Below is the code to implement this scenario.

successToastExample.js

 

 

successToastExample.html

OUTPUT

success variant

Error

In this scenario, we will be throwing an error to display an example of an Error Variant. We have another component here as errorToastExample

errorToastExample.js

 

errorToastExample.html

 

Error

 

Alert

In this scenario, we will be displaying an alerting type toast notification. We have created a component as “alertToastExample”

alertToastExample.js

 

infoToastExample.html

OUTPUT

Alert

Also Read – How to Use the getRecord Method in Salesforce LWC

Info

In this scenario, we will be displaying an  Info type variant for that we have created a component “infoToastExample”

infoToastExample.js

 

infoToastExample.html

 

OUTPUT

Info

 

Understand different toast attributes:

  • title:  The title attribute specifies the heading of the toast message.
  • message:  The message attribute holds the text displayed inside the toast notification.
  • variant: The variant attribute defines the type of toast message. Available options: success, error,     warning, info.
  • messageData: The messageData attribute allows dynamic content inside messages using placeholders.
  • mode:  The mode attribute controls how the toast behaves:
  • dismissible: (default): Users can manually close the toast.
  • pester: The toast stays until the user closes it.
  • sticky: The toast remains until an action is taken.

Now let’s take a practical where we can use different variants as per the scenario

Scenario – Here will be creating a record imperatively using Apex. We will display the different variants of Toast. Hence, showing success If the record is successfully created, Error if it throws any error with an error message, Alert and Info as needed.

createAccountRecord.js

 

In the above javascript file, we imported the apex class to insert the account record and also imported ShowToastEvent to display pop-up message to the user as per the scenario. Likewise, we have used error type for our required fields which are Account Name and Phone. The industry field here is not required but we want to let the user know that this field is empty. For the Website, we just use Info type to display that Adding website details would be good but would not restrict users from creating the records.

createAccountRecord.html

OUTPUT

output 1
output 2

AccountController.cls

In the above code, we have created an apex to insert account records with various inputs and also we are returning the newly inserted record id.

FAQs

1. What is the use of Toast Notification in LWC?

Toast notifications in LWC provide real-time feedback to users about various events, such as success messages, errors, warnings, or general information.

2. How do I handle errors using toast notifications?

You can display error messages by catching exceptions and passing the error message to the toast notification with the variant set to ‘error’.

3. Do toast notifications work in Aura components?

Yes, toast notifications work in Aura; however, the syntax differs compared to LWC. In Aura components, you need to use force:showToast to display toast messages.

Conclusion

In conclusion, Toast Notification in LWC is a powerful feature that enhances user experience by providing instant feedback. Whether you need to show success, error, warning, or informational messages, toast notifications help make interactions smoother. Furthermore, with different attributes available, they contribute to making your Salesforce applications more interactive and user-friendly.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Kashish have extensive Salesforce development experience, holding 4 Salesforce certifications. she posses expertise in Apex, Lightning Web Components, and Salesforce Admin, with a track record of successful project delivery. As a dedicated Salesforce enthusiast, she actively seek and embrace new challenges and opportunities within the dynamic Salesforce ecosystem.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *