Display Toast Notification with an Icon, Label, Message, and Links

Toast notifications are a crucial part of providing timely information and feedback to users in web applications. With the Salesforce Winter’24 release, you can now leverage the power of <strong>lightning/toast</strong> and <strong>lightning/toastContainer</strong> modules to create and manage toast notifications on Lightning Web Runtime (LWR) sites. In this blog post, we will dive into the process of creating and managing toast notifications using these modules.

Introduction to Toast Notifications

Toast notifications are small, non-intrusive messages that provide users with important information, alerts, or updates about their actions in a web application. Salesforce’s Experience Cloud now allows you to utilize the <strong>lightning/toast</strong> module to create these notifications with ease.

Getting Started

To begin, you’ll need to create a Lightning Web Component (LWC) that will handle the toast notifications. Let’s call it <strong>MyToastComponent</strong>.

myToastComponent.html

This HTML template defines the layout of the component. Inside a lightning-card, there are three lightning-button elements. Each button triggers a different toast notification when clicked.

<template>
<lightning-card title="Toast Notifications" icon-name="utility:notification">
<div class="slds-grid" style="margin-left: 4%;">
<div class="slds-col slds-size_4-of-12">
<lightning-button label="Toast Message 1" onclick={showToast1} variant="brand"></lightning-button>
</div>
<div class="slds-col slds-size_4-of-12">
<lightning-button label="Toast Message 2" onclick={showToast2} variant="brand"></lightning-button>
</div>
<div class="slds-col slds-size_4-of-12">
<lightning-button label="Toast Message 3" onclick={showToast3} variant="brand"></lightning-button>
</div>
</div>
</lightning-card>
</template>

myToastComponent.js

// c/myToastComponent.js
import { LightningElement } from 'lwc';
import ToastContainer from 'lightning/toastContainer';
import Toast from 'lightning/toast';
import LightningAlert from 'lightning/alert';
export default class MyToastComponent extends LightningElement {
toastContainer;
connectedCallback() {
this.toastContainer = ToastContainer.instance();
}
showToast1() {
this.toastContainer.maxToasts = 5;
this.toastContainer.toastPosition = 'top-left';
Toast.show({
label: 'Learn how to {0}',
labelLinks: [{
'url': 'https://sfdclesson.com/2023/08/31/display-toast-notification-with-an-icon-label-message-and-links/&#39;,
'label': 'Create Toast Notifications with Icons, Labels, and Links'
}],
message: 'Elevate your Salesforce skills with {0}',
messageLinks: [{
url: 'https://sfdclesson.com/&#39;,
label: 'SFDCLesson'
}],
mode: 'sticky',
variant: 'success',
onclose: () => {
LightningAlert.open({
message: 'this is the alert message',
theme: 'inverse', // a red theme intended for error states
label: 'Header!', // this is the header text
});
//Alert has been closed
}
}, this);
}
showToast2() {
//Sets the maximum number of toast components shown at a given time.
this.toastContainer.maxToasts = 5;
//Controls the position of toast components inside the toast container. Supported values are 'top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', and 'bottom-right'.
this.toastContainer.toastPosition = 'top-center';
Toast.show({
label: 'Learn how to {0}',
labelLinks: [{
'url': 'https://sfdclesson.com/2023/08/08/create-an-analog-clock-lightning-web-component-in-your-salesforce-org//&#39;,
'label': 'Create an Analog Clock Lightning Web Component in Your Salesforce Org'
}],
message: 'This message has a {0} placeholder link that gets replaced by from messageLinks',
messageLinks: [{
url: 'https://sfdclesson.com/&#39;,
label: 'SFDCLesson'
}],
mode: 'sticky',
variant: 'warning',
onclose: () => {
// Do something after the toast is closed
}
}, this);
}
showToast3() {
this.toastContainer.maxToasts = 5;
this.toastContainer.toastPosition = 'top-right';
Toast.show({
label: 'Learn how to create {0}',
labelLinks: [{
'url': 'https://sfdclesson.com/2023/07/11/custom-multi-select-picklist-in-lwc-with-drag-and-drop-feature/&#39;,
'label': 'Custom Multi-Select Picklist in LWC with Drag-and-Drop Feature'
}],
message: 'This message has a {0} placeholder link that gets replaced by from messageLinks',
messageLinks: [{
url: 'https://sfdclesson.com/&#39;,
label: 'SFDCLesson'
}],
mode: 'sticky',
variant: 'error',
onclose: () => {
// Do something after the toast is closed
}
}, this);
}
}

This JavaScript module (myToastComponent.js) contains the logic for creating and displaying toast notifications. Here’s a step-by-step breakdown of its key parts:

  1. Imports: Import necessary modules from the lightning namespace, including ToastContainer, Toast, and LightningAlert.
  2. Class Definition: The MyToastComponent class extends LightningElement and contains the component’s functionality.
  3. toastContainer Initialization: In the connectedCallback method, the toastContainer property is initialized with ToastContainer.instance() to obtain an instance of the toast container.
  4. showToast1(), showToast2(), showToast3() Methods: These methods are responsible for displaying different toast notifications with varying configurations. Each method sets the maxToasts and toastPosition properties of the toastContainer based on the desired positioning of the toast notifications.
  5. Toast.show() Method: Within each showToast method, the Toast.show() method is called to display a toast notification. The method takes an object as its first parameter, which defines the content and behavior of the toast notification. Key attributes used in the Toast.show() method include:
    • label: The title of the toast.
    • labelLinks: An array of links displayed in the title.
    • message: The message content of the toast.
    • messageLinks: An array of links displayed in the message.
    • mode: The mode of the toast (e.g., sticky).
    • variant: The appearance variant of the toast (e.g., success, warning, error).
    • onclose: A callback function executed after the toast is closed.
  6. LightningAlert: Within the onclose callback of showToast1(), a LightningAlert component is opened to display an alert message. This is an additional visual confirmation after the toast is closed.

In this code snippet, we first import the required modules, namely <strong>ToastContainer</strong> and <strong>Toast</strong>, from the <strong>lightning/toastContainer</strong> and lightning/toast packages, respectively. Inside the connectedCallback function, we initialised the <strong>toastContainer</strong>.

The <strong>showToast</strong> function demonstrates how to create a toast notification. It includes parameters such as <strong>label</strong>, <strong>labelLinks</strong>, <strong>message</strong>, and <strong>messageLinks</strong> that allow you to customize the content of the toast. The mode attribute sets the display mode, and variant specifies the appearance of the toast. The onclose callback can be used to execute actions when the toast is closed.

Working with ToastContainer

The lightning/toastContainer module enables you to manage and display a container that holds your toast notifications. This provides more control over how the toast notifications are presented to users. With lightning/toastContainer, you can:

  • Set the maximum number of toasts in the container using the maxToasts attribute.
  • Position the container using the containerPosition attribute.
  • Position the toast components within the container using the toastPosition attribute.

Important Note

While using lightning/platformShowToastEvent is a common method for displaying toast notifications in Salesforce Lightning Web Components, it’s essential to note that this approach is not supported on LWR sites. Instead, you should use the lightning/toast and lightning/toastContainer modules as described in this blog post.

In conclusion, creating and managing toast notifications using the lightning/toast and lightning/toastContainer modules on Salesforce LWR sites offers a powerful way to communicate important information to users in a user-friendly and non-disruptive manner. By leveraging these modules, you can enhance the user experience and streamline communication in your web applications.

References:

https://developer.salesforce.com/docs/component-library/bundle/lightning-toast-container/documentation

About the blog

SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:

Newsletter

Subscribe to our email newsletter to be notified when a new post is published.

Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

This Post Has One Comment

  1. Sreedhar Reddy

    Kudos to you for another fantastic blog post! Your ability to break down complex subjects into understandable, actionable steps is commendable. Your content continues to be a valuable resource for readers like me.
    Online Salesforce Training

Leave a Reply