Sunday 29 August 2021

How to show success message and error message in LWC?

1. First import ShowToastEvent 
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
2. Add below method.
showSuccess(){
        this.dispatchEvent(new ShowToastEvent({
          title: 'Success',
          message: 'Lead has been Cloned',
          variant: 'success',
        }));
    }

3. Call this.showSuccess(); where we want to show success message.
4. Error message
showError(msg){
        const evt = new ShowToastEvent({
            title: 'Eligibility Error',
            message: msg,
            variant: "error"
        });
        this.dispatchEvent(evt);
    }
5. Call from catch block to show error message
 .catch(err => {
                    console.log('error', err);
                    this.loading = false;
                    this.showError(err.body.message);
                });
if needed we can show generic error message instead of technical
error message.

No comments: