How to display dynamic toast message with custom button functionality in lwc | Toast event to pass dynamically attribute value as @track variable in LWC — Lightning Web Component

1,815 views

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 →

dynamic Toast Event in lwc

 

You can download file directly from github by Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 
 

 

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]

  1.   <template>
  2.     <lightning-card title="Toast Action" >
  3.     <div class="slds-col_bump-left slds-text-align_right slds-p-horizontal_x-small slds-text-align_center">
  4.         <lightning-button variant="brand" label="Toast Action" title="Toast Action"
  5.             class="slds-m-left_x-small slds-p-right_xx-small" onclick={toastAction}>
  6.         </lightning-button>
  7.     </div>
  8. </lightning-card>
  9. </template>

 

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : dynamicToastEvent.js

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

SFDX:Lightning Web Component >> New >> dynamicToastEvent.js

dynamicToastEvent.js [LWC JavaScript File]

  1.   import { LightningElement, track } from 'lwc';
  2. import { ShowToastEvent } from 'lightning/platformShowToastEvent';
  3. export default class DynamicToastEvent extends LightningElement {
  4.  
  5.     @track title;
  6.     @track message;
  7.     @track variant;
  8.     @track mode;
  9.  
  10.     //Mode Valid values are: dismissible (default), pester, sticky
  11.  
  12.     toastEventFire(title, msg, variant, mode) {     
  13.  
  14.         const toastEvent = new ShowToastEvent({            
  15.             title:title,
  16.             message:msg,
  17.             variant:variant,
  18.             mode:mode,
  19.            });
  20.            this.dispatchEvent(toastEvent);
  21.     }
  22.  
  23.  
  24.  
  25.     toastAction(event){
  26.         this.title='SUCCESS';
  27.         this.message= 'Record created successfully';
  28.         this.variant='SUCCESS';
  29.         this.mode='sticky',   
  30.  
  31.         this.toastEventFire(this.title, this.message, this.variant, this.mode);
  32.  
  33.     }
  34.  
  35. }

 

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]

  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>

 

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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