How to Redirect to another record from a Toast Message

Lets take an example that on a case record a new task is created, Once the Task is created we need to show the Toast Message as a link to a newly created Task record.


import {ShowToastEvent } from 'lightning/platformShowToastEvent';
import CreateTask from '@salesforce/apex/PLGPTaskCreationForCaseCtrl.CreateTaskWhenCaseContractTermination';

export default class PLGPAlertCaseCancellationOfContractIermination extends LightningElement {

@api recordId;

a_Record_URL;
var toast_title;
var toast_message;
var toast_messageData;
var toast_variant;
var toast_mode;

connectedCallback(){
        this.a_Record_URL = window.location.origin;
    }

CreateTask ({ accountId: accountId, ownerId:ownerId })
.then(result => {                              
                    if(result !=null && result.length == 1){
                        toast_title ='Success!';
                        toast_message = "{1}!";
                        toast_messageData =[
                            'Salesforce',
                            {
                                url: this.a_Record_URL +'/'+result[0].Id,
                                label: 'Task created'
                            }
                        ]
                        toast_variant = 'success';
                        toast_mode = 'sticky';
                        this.showToastMsg(toast_title, toast_message, toast_messageData,toast_variant, toast_mode);                      
                    }// if closed                                        
                })
                .catch(error => {
                    toast_title ='Error occured while creating a Task!';
                    toast_message ='REASON:'+error.body.message;
                    toast_messageData =null;
                    toast_variant = 'error';
                    toast_mode = 'dismissable';
                    this.showToastMsg(toast_title, toast_message, toast_messageData,toast_variant, toast_mode);
                })

// function
showToastMsg(title, message, messageData, variant, mode) {
        const evt = new ShowToastEvent({
            title: title,
            message: message,
            messageData:messageData,
            variant: variant,
            mode: mode
        });
        this.dispatchEvent(evt);
    }//showToastMsg function closed 

}// class closed

Leave a comment