+ Start a Discussion
fiona gentryfiona gentry 

How to send a default toast message in Lightning Web component(LWC) as Record Page is Loaded

I need to send  a information toast message in Lightning Web component(LWC) as Record Page is Loaded 
without any button click or change event

Below link has code but i dont need onclick ,it should show message by default
<lightning-button label="Show Info" onclick={showInfoToast}></lightning-button>

LWC recipes (https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_toast)

How to get this default Toast message in LWC?

Thanks for your help
Fiona
Best Answer chosen by fiona gentry
ravi soniravi soni
hi fiona,
By Using connectedCallback in lwc, you can achieve this requirment.
Below code will help you.
<template>
//Write your Html Logic here
</template>
########################################################
import { LightningElement } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class showToast extends LightningElement {

  connectedCallback() {
   var toast = new ShowToastEvent({
            'title': 'Sample Toast',
            'message': 'Page is loading successfully!!'
        });
        this.dispatchEvent(toast);
  }

}

don't forget to mark it as best answer.
Thank you
 

All Answers

CharuDuttCharuDutt
Hii Fiona
Try Below Code
<template>
Show Default Toast
</template>
########################################################
import { LightningElement } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class Test Component extends LightningElement {

  connectedCallback() {
   var toast = new ShowToastEvent({
            'title': 'Sample Toast',
            'message': 'This Is Simple Message'
        });
        this.dispatchEvent(toast);
  }

}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi, fiona gentry,
You can define the toast message inside the connectedCallback() method in the Lightning Web component(LWC) as Loaded without any button click. Please follow this sample code for your reference.
import { LightningElement } from 'lwc';
import { createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';

export default class LifeCycle extends LightningElement {
   
    connectedCallback(){
        const fields = {};
        fields[NAME_FIELD.fieldApiName] = 'account created after from connectedcallback today';
        const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields };
        createRecord(recordInput)
            .then(account => {
                console.log('account.id;' + account.id);
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Account created',
                        variant: 'success',
                    }),
                );
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating record',
                        message: error.body.message,
                        variant: 'error',
                    }),
                );
            });
   }    
}

Please mark it as the best answer, if it helps.
Thanks.
ravi soniravi soni
hi fiona,
By Using connectedCallback in lwc, you can achieve this requirment.
Below code will help you.
<template>
//Write your Html Logic here
</template>
########################################################
import { LightningElement } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class showToast extends LightningElement {

  connectedCallback() {
   var toast = new ShowToastEvent({
            'title': 'Sample Toast',
            'message': 'Page is loading successfully!!'
        });
        this.dispatchEvent(toast);
  }

}

don't forget to mark it as best answer.
Thank you
 
This was selected as the best answer