Hey guys, today in this post we are going to learn about How to display dynamic toast message with custom button functionality in LWC.
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, To know more details, Click Here.
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Lightning Web Component HTML →
Step 1:- Create Lightning Web Component : dynamicToastEvent.htm
SFDX:Lightning Web Component >> New >> dynamicToastEvent.htm
dynamicToastEvent.htm [Lightning Web Component HTML]
<template>
<lightning-card title="Toast Action" >
<div class="slds-col_bump-left slds-text-align_right slds-p-horizontal_x-small slds-text-align_center">
<lightning-button variant="brand" label="Toast Action" title="Toast Action"
class="slds-m-left_x-small slds-p-right_xx-small" onclick={toastAction}>
</lightning-button>
</div>
</lightning-card>
</template>
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : dynamicToastEvent.js
SFDX:Lightning Web Component >> New >> dynamicToastEvent.js
dynamicToastEvent.js [LWC JavaScript File]
import { LightningElement, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class DynamicToastEvent extends LightningElement {
@track title;
@track message;
@track variant;
@track mode;
//Mode Valid values are: dismissible (default), pester, sticky
toastEventFire(title, msg, variant, mode) {
const toastEvent = new ShowToastEvent({
title:title,
message:msg,
variant:variant,
mode:mode,
});
this.dispatchEvent(toastEvent);
}
toastAction(event){
this.title='SUCCESS';
this.message= 'Record created successfully';
this.variant='SUCCESS';
this.mode='sticky',
this.toastEventFire(this.title, this.message, this.variant, this.mode);
}
}
Create Lightning Web Component Meta XML →
Step 3:- Create Lightning Web Component : dynamicToastEvent.js-meta.xml
SFDX:Lightning Web Component >> New >> dynamicToastEvent.js-meta.xml
dynamicToastEvent.js-meta.xml [LWC Meta Data XML]
<?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 Salesforce
How do I create a toast event in LWC?
To trigger a toast from a Lightning web component, in the component's JavaScript class, import ShowToastEvent from lightning/platformShowToastEvent . Create a ShowToastEvent with a few parameters, and dispatch it.
How can we display toast messages in an activity?
Use the makeText() method, which takes the following parameters: The application Context . The text that should appear to the user.
What are the different toast types in Salesforce?
The toast type, which can be 'error', 'warning', 'success', or 'info'. The default is 'other', which is styled like an 'info' toast and doesn't display an icon, unless specified by the key attribute.
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 |