function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shreya KakadeShreya Kakade 

how to add toast success message in lightning component

Hello Everyone,

I am trying to add the Toast message to the lightning Component But its not working here is the my code of the Function.

deteteQuestion : function(component, event, helper){
        
        var action = component.get("c.delSlctRec");
        var idx = event.target.id;
        action.setParams({ "slctRec" : idx});
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
                        title: "Success!",
                        message: "Congrats, it works!",
                        type: "success"
                    });
        //var result = confirm("Are you sure you want to delete?");
       // if(toastEvent) {
            action.setCallback(this, function(response){
                var state =  response.getState();
                if(state === "SUCCESS")
                {
                    component.set("v.ques",response.getReturnValue());
                    
                    toastEvent.fire();
                    //document.getElementById("msg").innerHTML = "Record deleted successfully";
                    //alert('Successfully Deleted.');
                    // window.location.reload();
                    
                }else if (state=="ERROR") {
                    //console.log(action.getError()[0].message);
                    alert('error');
                }
            }); 
            
            $A.enqueueAction(action);
       // }
    },
Veena Sundara-HeraguVeena Sundara-Heragu
The JSON argument for toastEvent.setParams needs quotes around title, message and type like this:

        toastEvent.setParams({
                        "title": "Success!",
                        "message": "Congrats, it works!",
                        "type": "success"
                    });