Sub topic 3
Adding a link to a toast message
This chapter shows how to put a clickable link inside a toast. It assumes you can already dispatch a ShowToastEvent with a title, message, and variant. It also assumes you know basic LWC structure, @api, and dispatchEvent.
What you will learn
- How
messageDatafills placeholders inmessage. - How to turn a placeholder into a link with
urlandlabel. - Which toast mode suits a toast that contains a link.
- The common mistakes that make a link or placeholder fail.
Why put a link in a toast
A toast disappears. After a user saves a record, you often want to give them a way to reach it. A link in the toast does that without forcing a page change. The user stays where they are and can open the new record when they choose.
You do this with two properties of ShowToastEvent: message and messageData.
How message and messageData work together
The message property is a string. It can contain placeholders written as {0}, {1}, and so on. The messageData property is an array. Each placeholder is replaced by the array item at the same index.
An item in messageData can be one of two things:
- A plain string. It replaces the placeholder as text.
- An object with
urlandlabel. It replaces the placeholder with a link. Thelabelis the visible text, and theurlis where the link goes.
A complete example
The component below shows a sticky success toast after a record is created. The toast has the record name as text and an "Open it" link.
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ToastWithLink extends LightningElement {
@api recordId;
showCreated(name) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Record created',
message: '{0} was created. {1}',
messageData: [
name,
{
url: '/lightning/r/Account/' + this.recordId + '/view',
label: 'Open it'
}
],
variant: 'success',
mode: 'sticky'
})
);
}
}Read it in three parts.
messagehas two placeholders:{0}and{1}.messageData[0]is the stringname, so{0}becomes the record name.messageData[1]is an object, so{1}becomes a link that reads "Open it".
The rendered toast reads like this: "Acme Corp was created. Open it", where "Open it" is clickable.
Choosing a mode for a toast with a link
The mode property controls how long the toast stays. A toast with a link has a special need: the user must have time to click it.
| Mode | Behavior | Fit for a toast with a link |
|---|---|---|
| `dismissable` | Disappears after a short time, and has a close button. | Risky. The link may vanish before the user reads it. |
| `pester` | Disappears after a short time, with no close button. | Risky for the same reason. |
| `sticky` | Stays until the user closes it. | Best choice. |
The default mode is dismissable. The example above uses sticky for this reason.
Note that the first code example in the earlier chapter used the value dismissible. Check the spelling your org accepts against the current Salesforce documentation before you rely on a mode name, because a value that is not recognized falls back to the default behavior rather than raising an error.
Where the url can point
The url value is a string. Two forms are common.
- A relative path inside Lightning Experience, such as
/lightning/r/Account/001.../view. This is what the example uses. - A full external address, such as
https://example.com/help.
Building the path by hand, as in the example, ties your code to the current URL format. A safer option is to ask the platform for the URL. NavigationMixin.GenerateUrl takes a page reference and returns a URL string through a Promise. A later chapter covers it in detail. Here is how it combines with a toast.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ToastWithGeneratedLink extends NavigationMixin(LightningElement) {
@api recordId;
async showCreated(name) {
const url = await this[NavigationMixin.GenerateUrl]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
actionName: 'view'
}
});
this.dispatchEvent(
new ShowToastEvent({
title: 'Record created',
message: '{0} was created. {1}',
messageData: [name, { url, label: 'Open it' }],
variant: 'success',
mode: 'sticky'
})
);
}
}The url shorthand in { url, label: 'Open it' } works because the variable is named url. The toast is dispatched only after the Promise resolves, so the link always has a value.
Common errors
Placeholder text appears literally
If the toast shows {0} or {1} as written, messageData is missing or is not an array. Check that you passed an array and that you spelled the property messageData.
The number of placeholders and items differ
A placeholder with no matching item has nothing to display. Keep the counts equal, and keep the indexes in order starting at 0.
The link is an object printed as text
Passing a plain object where a string is expected, for example putting an object directly in message, does not create a link. Only an object placed in messageData is turned into a link.
The link works once, then the toast is gone
This is the behavior of dismissable and pester modes. Switch to sticky.
The record ID is undefined
In the example, the URL is built from this.recordId. If the component is not placed on a record page, recordId is undefined, and the link points to a broken path. Use the ID of the record you just created, or guard against a missing value before you build the URL.
if (!this.recordId) {
return;
}Security and content notes
The message text is a template, not HTML. You cannot place a tag such as an anchor element inside it. This is intentional, and it is why messageData exists. When you insert user-provided text, put it in messageData as a plain string. That keeps the value as text and not as markup.
A callout on label text:
Summary of the pattern
- Write
messagewith a numbered placeholder where the link should appear. - Add
messageDataas an array with one item for each placeholder. - Make the link item an object with
urlandlabel. - Use
mode: 'sticky'so the user has time to click. - Prefer
GenerateUrlover hand-built paths when you can.
Exercise
A toast reads "{0} was saved. {1}" in the browser, with the braces visible and no link. The component passes messageData: { url: '/lightning/r/Account/001/view', label: 'Open it' }. What is wrong, and how do you fix it?
Show answer
messageData must be an array, but here it is a single object. Wrap the values in an array with one item for each placeholder, for example messageData: ['Acme', { url: '/lightning/r/Account/001/view', label: 'Open it' }]. Also set mode: 'sticky' so the link stays available long enough to click.
Exercise
Why is sticky a better mode than dismissable for a toast that contains a link?
Show answer
A sticky toast stays on screen until the user closes it, so the user has time to read the message and click the link. A dismissable toast closes after a short time and the link can disappear first.