I have created a LWC that executes a call to the controller and shows a toast message if the call fails:
const event = new ShowToastEvent({
title: "Error",
message: "An error has occured. Return to record.",
variant: "error"
});
this.dispatchEvent(event);
Return to record should be a hyperlink. Still, if I build the message to include the hyperlink, the string gets escaped and the hyperlink doesn't get properly displayed. I have checked the documentation and couldn't find a way to do that, even though in aura it was available.
Is there any way to achieve this? Or it's simply not supported yet?
Edit: I have managed to include a Return to Record link in the toast using Jayant's answers below. Here is the code that does that:
AuraContainer.cmp:
<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">
<c:sendemail onshowtoast="{!c.handleShowToast}"/>
</aura:component>
AuraContainer.js:
({
handleShowToast : function(component, event, helper) {
var message = event.getParam('message');
var sObjectEvent = $A.get("e.force:navigateToSObject");
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
title: message.title,
mode: message.mode,
type: message.variant,
message: 'Sample Message', // you will need to keep this attribute, even though it's not used
messageTemplate: '{0} {1}',
messageTemplateData: [
message.text,
{
url: '',
label: 'Return to Org',
executionComponent: {
descriptor: "markup://force:navigateToSObject",
attributes: {recordId: message.recordId, slideDevName:"detail"},
isEvent: true,
isClientSideCreatable: true,
componentName: 'navigateToSObject'
}
}
]
});
toastEvent.fire();
}
})
SendEmail LWC:
import { LightningElement, api, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import checkOrgValidity from '@salesforce/apex/SendEmailController.checkOrgValidity'
import { ShowToastEvent } from 'lightning/platformShowToastEvent'
export default class SendEmail extends LightningElement {
@api recordId;
// Injects the page reference that describes the current page
@wire(CurrentPageReference)
currentPageReference;
async connectedCallback() {
this.recordId = this.currentPageReference.state.c__recordId;
checkOrgValidity({sId: this.recordId})
.then(result => {
// processing in case of success
})
.catch(error => {
var errors = JSON.parse(error.body.message);
for(var i = 0; i < errors.length; i++) {
// make sure that the message is not empty
if(errors[i]) {
this.showToastWithHyperlink(errors[i], this.recordId, 'Warning', 'warning', 'sticky');
}
}
});
}
showToastWithHyperlink(msg, recordId, title, variant, mode) {
let message = {};
message.title = title;
message.text = msg;
message.recordId = recordId;
message.mode = mode;
message.variant = variant;
const showToast = new CustomEvent('showtoast', {
detail: { message },
});
this.dispatchEvent(showToast);
}
}
The Lightning component gets opened using a Detail Page Button:
Behavior: Display in existing window with sidebar
Button or Link URL: /lightning/cmp/c__SendEmailContainer?c__recordId={!Account.Id}
force:showToast
used for Lightning Aura Components supportedmessageTemplate
andmessageTemplateData
for this purpose, but it doesn't seem to have an impact with LWC, the docs doesn't mention this either.messageTemplate
andmessageTemplateData
and it didnt work. You can work. You have to wrap inside an aura component and use pub - sub to child-to parent to fire event and aura parent will listen and fireshowToast