How to Show Toast Message in Aura Component: A Step-by-Step Guide

Showing toast messages in Aura components is a useful way to provide notifications and alerts to users. Whether you need to display a success message after a record is saved or show an error message when an operation fails, toast messages are a handy tool to have in your developer toolkit. In this step-by-step guide, we will walk you through the process of showing toast messages in Aura components, so you can enhance the user experience of your Salesforce applications.

Prerequisites

Before we dive into the implementation details, let’s make sure we have all the necessary prerequisites in place. Firstly, you should have a basic understanding of Salesforce and the Aura framework. Familiarity with JavaScript, HTML, and CSS will also be beneficial. Additionally, you will need access to a Salesforce org where you can create and modify Aura components.

Step 1: Create an Aura Component

The first step in showing toast messages is to create an Aura component. You can do this by navigating to the Developer Console in your Salesforce org. From there, select “File” and then “New”, followed by “Aura Component”.

Give your component an appropriate name and choose the desired access level. Click on the “Submit” button to create the component.

Step 2: Add the Required Markup

Next, open the newly created Aura component file and add the necessary markup to display the toast message. Start by adding the tag and its attributes. Within the component, you will need to include a button or link that triggers the toast message when clicked. For example, you can add a button with an onclick attribute that calls a JavaScript controller function.

Example Markup:

“`html


“`

Step 3: Implement the JavaScript Controller

Now it is time to implement the JavaScript controller for handling the toast message logic. In the same Aura component file, locate the JavaScript controller section and define the function that will display the toast message. This function will be called when the button or link is clicked.

Example JavaScript Controller:

“`javascript
({
showToast : function(component, event, helper) {
var toastEvent = $A.get(“e.force:showToast”);
toastEvent.setParams({
“title”: “Success!”,
“message”: “Record saved successfully.”,
“type”: “success”
});
toastEvent.fire();
}
})
“`

In the above example, we are using the `force:showToast` event to display the toast message. The parameters of this event include the title, message, and type of the toast message. You can customize these values based on your requirements.

Step 4: Test and Verify

Once you have added the required markup and implemented the JavaScript controller, it’s time to test and verify the toast message functionality. Save the Aura component file and navigate to a page or app where you have included the component. Click on the button or link that triggers the toast message and observe the result.

If everything is working correctly, you should see a toast message with the specified title and message. The type of the message (e.g. success, error, warning) will also be reflected through the styling and icon.

Customizations and Advanced Usage

Although we have covered the basic functionality of showing toast messages in Aura components, there are additional customizations and advanced usage options available. Here are a few examples:

Customizing the Toast Type:

By changing the value of the `type` attribute in the JavaScript controller, you can display different types of toast messages. The available options include “success”, “error”, “warning”, and “info”. Experiment with these values to achieve the desired look and feel.

Displaying Long Messages:

If you need to display a longer message in the toast, you can include line breaks (`n`) within the `message` parameter. This will ensure that the message is displayed properly with multiple lines of text.

Handling Toast Events:

In addition to showing toast messages, you can also listen for and handle toast events within your Aura components. This can be useful if you need to perform additional actions or logic based on the user’s interaction with the toast message. For example, you can capture the user’s click on the close button of the toast message and trigger a specific action.

Conclusion

In conclusion, showing toast messages in Aura components is a straightforward process that can greatly enhance the user experience of your Salesforce applications. By following the steps outlined in this guide, you can easily implement toast message functionality and customize it to suit your specific needs. With these notifications and alerts, you can keep your users informed and engaged throughout their interactions with your application. Happy coding!

Leave a Comment