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.
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:
- Success – Displays a success message.
- Error – Shows an error message.
- Info – Displays a warning message.
- 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:
- Right-clicking on the lwc folder and then creating the lightning web component
- Another way is Cmd/Ctrl +Shift + P and then open the SFDX command to create a Lightning web 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
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
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
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
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
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
createAccountRecord.html
OUTPUT
AccountController.cls
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.