Skip to content
  • linkedIn
  • twitter
  • About Us
  • News letter
  • Privacy Policy
  • Contact Us
sfdcgenius

sfdcGenius

Learn Practice and Execute

salesforce
  • Home
  • Blog
  • Apex
  • Toggle search form
LightningAlert, LightningConfirm, and LightningPrompt in Lightning web components.

LightningAlert, LightningConfirm, and LightningPromt in Lightning web components.

Posted on February 9, 2023February 9, 2023 By sfdcGenius No Comments on LightningAlert, LightningConfirm, and LightningPromt in Lightning web components.

LightningAlert, LightningConfirm, and LightningPrompt are used to create notifications from your Lightning web components. Chrome and Safari plan to end support for cross-origin use of the window.alert(),  window.confirm() and window.prompt() native APIs.
Each new module creates a modal that functions like its API counterpart and works in cross-origin iframes.

LightningAlert, LightningConfirm, and LightningPrompt return a promise. Use async/await or .then() for any code that you want to execute after the modal closes.

Table of Contents

  • Type of Notification in Lightning Web Component.
    • LightningAlert :
      • LightningAlert.html
      • createNotification.js
    • LightningConfirm :
      • LightningConfirm.html
      • LightningConfirm.js
    • LightningPrompt :
      • LightningPrompt.html
      • LightningPrompt.js

Type of Notification in Lightning Web Component.

  1. LightningAlert
  2. LightningConfirm 
  3. LightningPrompt

do you know What are Decorators in Lightning Web Component (LWC)? Decorators in LWC

LightningAlert :

LightningAlert creates an alert modal with an error message and an OK button. The .open( ) function returns a promise that resolves when the user clicks OK.

LightningAlert.html

<!-- LightningAlert.html -->
<template>
    <lightning-button onclick={handleAlertClick} label="Open Alert Modal">
    </lightning-button>
</template>

createNotification.js

// LightningAlert.js 
import { LightningElement } from 'lwc';
import LightningAlert from 'lightning/alert';
export default class LightningAlert extends LightningElement {
    async handleAlertClick() {
        await LightningAlert.open({
            message: 'this is the alert message',
            theme: 'error', // a red theme intended for error states
            label: 'Error!', // this is the header text
        });
        //Alert has been closed
    }
}
LightningAlert

LightningConfirm :

LightningConfirm creates a confirm modal with two buttons, OK and Cancel. The .open( ) function returns a promise that resolves to true when the user clicks OK and false when they click Cancel.

LightningConfirm.html

<!-- LightningConfirm.html -->
<template>
    <lightning-button onclick={handleConfirmClick} label="Open Confirm Modal">
    </lightning-button>
</template>

LightningConfirm.js

// LightningConfirm.js 
import { LightningElement } from 'lwc';
import LightningConfirm from 'lightning/confirm';
export default class LightningConfirm extends LightningElement {
    async handleConfirmClick() {
        const result = await LightningConfirm.open({
            message: 'this is the confirm message',
            variant: 'header',
            label: 'Please Confirm',
            theme: 'success',
            //label: 'this is the aria-label value',
            // setting theme would have no effect
        }).then((result) => {
            //Prompt has been closed
            //result is input text if OK clicked
            //and null if cancel was clicked
            console.log('Lightning Confirm Result===>',result);
        });
    }
}
LightningConfirm

LightningPrompt :

LightningPrompt creates a prompt modal with a header, message, and two buttons. If the user inputs text and clicks OK in the prompt, the .open() function returns a promise that resolves to the input value, but if the user clicks Cancel it resolves to null.

LightningPrompt.html

<!-- LightningPrompt.html -->
<template>
    <lightning-button onclick={handlePromptClick} label="Open Prompt Modal">
    </lightning-button>
</template>

LightningPrompt.js

// LightningPrompt.js 
import { LightningElement } from 'lwc';
import LightningPrompt from 'lightning/prompt';
export default class LightningPrompt extends LightningElement {
    handlePromptClick() {
        LightningPrompt.open({
            message: 'this is the prompt message',
            //theme defaults to "default"
            label: 'Please Respond', // this is the header text
            defaultValue: 'Blog is fantastic', //this is optional
        }).then((result) => {
            console.log('Lightning Prompt Result===>',result);
            //Prompt has been closed
            //result is input text if OK clicked
            //and null if cancel was clicked
        });
    }
}
LightningPrompt

For any Queries/doubts comment below and for quick responses connect on LinkedIn and Twitter.

Related Post

Customization, Lightning Web Component, Ligthing, LWC, Recent Post Tags:lightning web component, LightningAlert, LightningConfirm, LightningPrompt, lwc, notification in lwc, Salesforce, trailhead

Post navigation

Previous Post: What are Decorators in Lightning Web Component (LWC)?
Next Post: Salesforce Flow

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Admin
  • Apex
  • Apex Class
  • Apex Triggers
  • Customization
  • Flow
  • Lightning Web Component
  • Ligthing
  • LWC
  • Recent Post
  • Salesforce Security

Tags

@api @track @wire Admin Apex Apex Class Apex Trigger api connectedCallback() decorators disconnectedCallback disconnectedCallback() errorCallback errorCallback() Flow How to check whether record values are changed or not in Apex Trigger how to create modal in lwc how to create popup in lwc lighting flow LightningAlert LightningConfirm LightningPrompt lightning web component lwc modal/popup in lwc Modal in lwc notification in lwc popup in lwc renderedCallback() Role Hierarchies Salesforce Salesforce Customization salesforce flow salesforce latest update salesforce layoff sfdc sfdcGenius sfdcginus track trailhead Trigger.NewMap Vs Trigger.oldMap trigger.old vs Trigger.New wire wrapper class in apex wrapper class in salesforce

Resources

  • About Us
  • Blog
  • Contact Us
  • Home
  • Newsletter
  • Privacy Policy

Social Media

  • linkedIn
  • twitter

Copyright © 2025 sfdcGenius.

Powered by PressBook WordPress theme