How to display different types of custom toast message as Error, Success, Info and warning toast notifications on click button, uses of ShowToastEvent property in Salesforce Lightning Web Component — LWC | display custom toast notification in lwc

5,048 views

Hey guys, today in this post we are going to learn about How to How to How to display different types of custom toast message as Error, Success, Info and warning toast message notifications on click button, uses of importing the ‘ShowToastEvent’ property in Javascript in Salesforce Lightning Web Component — LWC.

A lighting component send a toast notification that pops up to alert users of a success, error, or warning. A toast can also simply provide information. To display a toast notification in Lightning Experience or Experience Builder sites, import ShowToastEvent from the lightning/platformShowToastEvent module. Know more about toast notification in lwc

Parameters of toast notification

Why Should You Schedule Meeting?

🎯 If You Are Facing Any Of These 6 Challenges. Schedule Meeting With Me.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 
PARAMETER TYPE DESCRIPTION
title String (Required) The title of the toast, displayed as a heading.
message String (Required) A string representing the body of the message. It can contain placeholders in the form of {0} … {N}. The placeholders are replaced with the links on messageData.
messageData String[] or Object url and label values that replace the {index} placeholders in the message string.
variant String Changes the appearance of the notice. Toasts inherit styling from toasts in the Lightning Design System. Valid values are: info (default), success, warning, and error.
mode String Determines how persistent the toast is. Valid values are: dismissible (default), remains visible until you click the close button or 3 seconds has elapsed, whichever comes first; pester, remains visible for 3 seconds and disappears automatically. No close button is provided; sticky, remains visible until you click the close button.

Files we used to create custom toast notification in LWC

lwcDisplayToastMsg.html LWc HTML File Template HTML file to create different types of toast message in lwc
lwcDisplayToastMsg.js LWC JavaScript File It’s hold a javascript on click function to display the different types Toast Message Notifications using ‘ShowToastEvent’ element lwc.
lwcDisplayToastMsg.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage.

Final Output

display different types of toast message in lwc -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➡ lwcDisplayToastMsg.html

SFDX:Lightning Web Component ➡ New ➡ lwcDisplayToastMsg.html

lwcDisplayToastMsg.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card>
  3.         <div class="slds slds-p-around--medium">  
  4.         <h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom20" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px;"> Display Toast Notifications in Lightning Web Component (LWC) </strong></h3>
  5.          <br/><br/>
  6.  
  7.           <table class="slds-table slds-table_header-fixed slds-table_bordered slds-table_edit slds-table_resizable-cols" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:1px #ddd solid;">
  8.              <tbody> 
  9.               <tr>
  10.                   <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="destructive " label="Show Error" onclick={displayToastError}></lightning-button>    </td>
  11.                   <td style="width:70%; color:rgba(214, 5, 1, 1); font-weight:bold;">Display Error Toast Message Notifications</td>
  12.               </tr>
  13.               <tr>
  14.                 <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="brand" label="Show Warning" onclick={displayToastWarning}></lightning-button></td>
  15.                 <td style="width:70%; color:rgb(243 114 4); font-weight:bold;">Display Warning Toast Message Notifications</td>
  16.             </tr>
  17.             <tr>
  18.                 <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="success" label="Show Success" onclick={displayToastSuccess}></lightning-button></td>
  19.                 <td style="width:70%; color:rgb(21 136 0); font-weight:bold;">Display Success Toast Message Notifications</td>
  20.             </tr>
  21.             <tr>
  22.                 <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="brand" label="Show Info" onclick={displayToastInfo}></lightning-button></td>
  23.                 <td style="width:70%; color:rgb(212 205 0); font-weight:bold;">Display Info Toast Message Notifications</td>
  24.             </tr>
  25.           </tbody> 
  26.           </table>   
  27.         <br/><br/>
  28.         <p>To know more <strong>live demo</strong> in LWC, Use this <strong><a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">link..</a></strong></p>
  29.     </div>
  30. <br/><br/>
  31.    <!--Start RelatedTopics Section-->
  32. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  33.  
  34.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  35.  
  36.             <br/><br/>
  37.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  38.             <div style="display:block; overflow:hidden;"> 
  39.                 <div style="width: 50%; float:left; display:inline-block">
  40.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  41.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  42.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  43.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  44.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  45.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  46.                     </ul>
  47.             </div>
  48.  
  49.             <div style="width: 50%; float:left; display:inline-block">
  50.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  51.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  52.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  53.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  54.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  55.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  56.                     </ul>
  57.                 </div>
  58.                <div style="clear:both;"></div> 
  59.                <br/>
  60.                 <div class="youtubeIcon">
  61.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  62.                 </div>
  63.     </div>
  64.  
  65. </div>
  66.  
  67.   <!--End RelatedTopics Section-->
  68.  
  69.    </lightning-card> 
  70. </template>
  71.  
  72.     </lightning-card>
  73. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➡ lwcDisplayToastMsg.js

SFDX:Lightning Web Component ➡ New ➡ lwcDisplayToastMsg.js

lwcDisplayToastMsg.js [LWC JavaScript File]

  1.    import { LightningElement } from 'lwc';
  2. import { ShowToastEvent } from 'lightning/platformShowToastEvent';
  3.  
  4. export default class LwcDisplayToastMsg extends LightningElement {
  5.     displayToastError() {
  6.         const toastEvt = new ShowToastEvent({
  7.             title: 'Error',
  8.             message: 'Some Error Occurred',
  9.             variant: 'error',
  10.             mode: 'dismissable'
  11.         });
  12.         this.dispatchEvent(toastEvt);
  13.     }
  14.     displayToastSuccess() {
  15.         const toastEvt = new ShowToastEvent({
  16.             title: 'Success',
  17.             message: 'Submitted Successfully ',
  18.             variant: 'success',
  19.             mode: 'dismissable'
  20.         });
  21.         this.dispatchEvent(toastEvt);
  22.     }
  23.     displayToastWarning() {
  24.         const toastEvt = new ShowToastEvent({
  25.             title: 'Toast Warning',
  26.             message: 'Some Problem Occurred',
  27.             variant: 'warning',
  28.             mode: 'dismissable'
  29.         });
  30.         this.dispatchEvent(toastEvt);
  31.     }
  32.     displayToastInfo() {
  33.         const toastEvt = new ShowToastEvent({
  34.             title: 'Toast Info',
  35.             message: 'Data running in background',
  36.             variant: 'info',
  37.             mode: 'dismissable'
  38.         });
  39.         this.dispatchEvent(toastEvt);
  40.     }
  41. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➡ lwcDisplayToastMsg.js-meta.xml

SFDX:Lightning Web Component ➡ New >> lwcDisplayToastMsg.js-meta.xml

lwcDisplayToastMsg.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

display different types of toast message in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is a toast message in Salesforce?

Toast are the message box or you can say notification box which you can use to show messages/notification on top of the view. Toast displays a message below in the header at the top of the view.

How do you display a toast message in LWC?

To display a toast notification in Lightning Experience or Experience Builder sites, import ShowToastEvent from the lightning/platformShowToastEvent module.

What is connectedCallback in LWC?

The connectedCallback() is a lifecycle hook in lightning web component . It fires when a component is inserted into the DOM.

Related Topics | You May Also Like

 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration




Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment