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
- Do you know:- The cheapest and best smartphone in your budget 2022?
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
Other related post that would you like to learn in LWC
- Find the below steps
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<template> <lightning-card> <div class="slds slds-p-around--medium"> <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> <br/><br/> <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;"> <tbody> <tr> <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="destructive " label="Show Error" onclick={displayToastError}></lightning-button> </td> <td style="width:70%; color:rgba(214, 5, 1, 1); font-weight:bold;">Display Error Toast Message Notifications</td> </tr> <tr> <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="brand" label="Show Warning" onclick={displayToastWarning}></lightning-button></td> <td style="width:70%; color:rgb(243 114 4); font-weight:bold;">Display Warning Toast Message Notifications</td> </tr> <tr> <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="success" label="Show Success" onclick={displayToastSuccess}></lightning-button></td> <td style="width:70%; color:rgb(21 136 0); font-weight:bold;">Display Success Toast Message Notifications</td> </tr> <tr> <td style="width:30%; border-right:1px #ddd solid;"><lightning-button variant="brand" label="Show Info" onclick={displayToastInfo}></lightning-button></td> <td style="width:70%; color:rgb(212 205 0); font-weight:bold;">Display Info Toast Message Notifications</td> </tr> </tbody> </table> <br/><br/> <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> </div> <br/><br/> <!--Start RelatedTopics Section--> <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;"> <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> <br/><br/> <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> <div style="display:block; overflow:hidden;"> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <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> <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> <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> <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> <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> </ul> </div> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <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> <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> <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> <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> <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> </ul> </div> <div style="clear:both;"></div> <br/> <div class="youtubeIcon"> <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> </div> </div> </div> <!--End RelatedTopics Section--> </lightning-card> </template> </lightning-card> </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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import { LightningElement } from 'lwc'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; export default class LwcDisplayToastMsg extends LightningElement { displayToastError() { const toastEvt = new ShowToastEvent({ title: 'Error', message: 'Some Error Occurred', variant: 'error', mode: 'dismissable' }); this.dispatchEvent(toastEvt); } displayToastSuccess() { const toastEvt = new ShowToastEvent({ title: 'Success', message: 'Submitted Successfully ', variant: 'success', mode: 'dismissable' }); this.dispatchEvent(toastEvt); } displayToastWarning() { const toastEvt = new ShowToastEvent({ title: 'Toast Warning', message: 'Some Problem Occurred', variant: 'warning', mode: 'dismissable' }); this.dispatchEvent(toastEvt); } displayToastInfo() { const toastEvt = new ShowToastEvent({ title: 'Toast Info', message: 'Data running in background', variant: 'info', mode: 'dismissable' }); this.dispatchEvent(toastEvt); } } |
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 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>45.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle> |
Further post that would you like to learn in LWC
If you think you and your family members also like to earn money in additional time, then go this link. In this site, you can earn money for the lifetime at low effort. To Earn Unlimited Everyday With This Trick, Sign Up Free.. Get Started for Free
Check Out:- Earn Unlimited Everyday With This Trick:- Click Here
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
- Your reaction of the article ▾
It’s a knowledge information. Found it useful. Thank you for sharing this article with us. Please checkout my website.
Very interesting article, well written and useful. Looking forward to reading new articles on the subject!