0

Here is the logic which is working fine while using the button, but I'm trying to display the notification once the page is loaded. this logic which I tried,
js:

import { LightningElement,wire,track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import toastMsg from '@salesforce/apex/ToastCtrl.toastMessage';
export default class toastcmp extends LightningElement {
    @wire(toastMsg)
    toast;
    notification(){
        const evt = new ShowToastEvent({
            title: 'Message',
            message:this.toast.data.Message__c,
            variant: 'info'
        });
        this.dispatchEvent(evt);
    }
}

html:

<template>
   <lightning-button label="Click" onclick={notification}></lightning-button>
</template>

Can anyone provide the solution? I tried using this but it is showing an empty page, it is not working. connectedCallback(){ this.notification(); } Thanks, in advance.

2
  • 3
    What is meant by "but it is showing an empty page"? connectedCallback(){ this.notification(); } works fine on a lightning__RecordPage.
    – TSmith
    Dec 21, 2021 at 16:33
  • TSmith is correct - you have to call this.notification() inside the connectedCallback() for the toast to fire on component load. connectedCallback() is a standard function for LWCs that'll run on load. developer.salesforce.com/docs/component-library/documentation/… Dec 21, 2021 at 18:26

1 Answer 1

0

This should do it:

import {LightningElement, wire} from "lwc";
import {ShowToastEvent} from "lightning/platformShowToastEvent";
import toastMsg from "@salesforce/apex/ToastCtrl.toastMessage";
export default class toastcmp extends LightningElement {
    @wire(toastMsg)
    toast;

    notification() {
        const evt = new ShowToastEvent({
            title: "Message",
            message: this.toast.data.Message__c,
            variant: "info"
        });
        this.dispatchEvent(evt);
    }

    connectedCallback() {
        this.notification();
    }
}

html:

<template>
</template>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .