Sub topic 17
Putting it together: confirm, save, toast, and navigate
This chapter combines the three building blocks into one flow: a user clicks Delete, confirms in a modal, the record is deleted, a toast reports the result, and the user lands on a new page. It assumes you can already open a LightningModal, dispatch a ShowToastEvent, and call NavigationMixin.Navigate. It also assumes you are comfortable with async/await.
The flow
The order of the steps matters. The modal must resolve before any data changes, and navigation must happen only after the delete succeeds.
The modal
The modal template uses the header, body, and footer components. The footer holds the two buttons.
<template>
<lightning-modal-header label={label}></lightning-modal-header>
<lightning-modal-body>
<p>Delete {recordName}? This cannot be undone.</p>
</lightning-modal-body>
<lightning-modal-footer>
<lightning-button label="Cancel" onclick={handleCancel}></lightning-button>
<lightning-button variant="destructive" label="Delete" onclick={handleConfirm}></lightning-button>
</lightning-modal-footer>
</template>The class closes with a string. That string becomes the result the opener receives.
import { api } from 'lwc';
import LightningModal from 'lightning/modal';
export default class ConfirmDeleteModal extends LightningModal {
@api recordName;
handleCancel() {
this.close('cancel');
}
handleConfirm() {
this.close('confirm');
}
}The parent component
The parent extends NavigationMixin(LightningElement) so it can navigate. It awaits the modal, then deletes, then reports.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { deleteRecord } from 'lightning/uiRecordApi';
import ConfirmDeleteModal from 'c/confirmDeleteModal';
export default class DeleteAccount extends NavigationMixin(LightningElement) {
@api recordId;
async handleDelete() {
const result = await ConfirmDeleteModal.open({
size: 'small',
description: 'Confirm deletion of this account',
label: 'Confirm delete',
recordName: 'this account'
});
if (result !== 'confirm') {
return;
}
try {
await deleteRecord(this.recordId);
this.dispatchEvent(
new ShowToastEvent({
title: 'Deleted',
message: 'The account was deleted.',
variant: 'success'
})
);
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'home'
}
});
} catch (error) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Delete failed',
message: error.body ? error.body.message : error.message,
variant: 'error',
mode: 'sticky'
})
);
}
}
}Why it is built this way
- Early return on cancel.
openresolves with whateverclosereceived. Anything other than'confirm', including a dismissal with the close button, which resolves withundefined, stops the flow. - Toast before navigate. The toast is dispatched first, then navigation starts. Toasts are shown by the page container, so the message stays visible after the page changes.
- Different toast modes. The success toast uses the default mode and fades by itself. The error toast is
sticky, so the user has time to read the reason. - Error message fallback. Errors from Lightning Data Service carry details in
error.body.message. Other errors, such as a thrownError, only haveerror.message. The ternary handles both. - Navigate only inside the success path. If
deleteRecordthrows, control jumps tocatchand the user stays on the record page, which still exists.
Common errors
- Calling
this[NavigationMixin.Navigate]in a class that does not extendNavigationMixin(LightningElement). The call fails because the property does not exist. - Passing a value to
openthat has no matching@apiproperty in the modal. The value is ignored. - Forgetting
awaitonopen.resultis then a Promise, so the comparison with'confirm'is always false and nothing is deleted.
Exercise
In the delete flow, why is the error toast set to mode 'sticky' while the success toast uses the default mode, and where in the code must Navigate be called so the user is not sent away after a failed delete?
Show answer
A failure needs the user's attention and contains a reason they may want to read, so the sticky toast stays until dismissed. A success message is routine and can fade on its own. Navigate must be called inside the try block, after the awaited deleteRecord call succeeds. If deleteRecord throws, execution jumps to the catch block and the navigation line is never reached.