Application events with example

by | Oct 14, 2018 | Lightning | 8 comments

In my previous post I gave a brief theoretical introduction about the different lightning events we have in Salesforce. In this post I am gonna show how you can use the different types of events.


Application events:

As explained in my previous post, application events are the easiest to fire and handle. But as they said, with great power comes great responsibility. Application events while being easy to implement, can cause your application channels to get clogged if used unwisely and relentlessly. So lets see the basic structure of an application event.




This is what an application event looks like in its definition. The type determines whether this is an application event or a component event. In this case it is an APPLICATION event. Every event can carry a payload within itself. That way an event becomes even more useful because it is carrying information that could be useful to other components. In this case the component has a payload of type Student__c.


Event Registration:

Before we can fire an event, the firing component needs to register the event to let the framework know that it may fire an event of that type. without registering an event you cant fire it. This is not true about system events though. The framework already knows about them, you just need to fire them. Lets see how you can register an event.



Firing the event:

Once an event has been registered, the next step is firing the event. You can easily fire the event from your javascript controller or javascript helper. Let’s see how we can do this.



var updateStudentEvent = $A.get("e.c:updateStudents");
                        student : response.getReturnValue()

Easy right? Just get an instance of the application event, set value for the payLoad, in this case student and then ready, aim and fire!!!! That’s how you fire an application event.


Event Handling:

Once an event has been fired, it also needs to be handled. Without a suitable handler, it is pointless to fire an event. So lets see how does a basic handler registration looks like.


This is how you register a handler for an event. You specify the event with proper namespace (c: in this context) and a controller method which handles this event. Now let’s take a look at the event handler in the JS controller file.


handleUpdateStudents : function(component, event, helper) {
		console.log("Handling update event");
        var student = event.getParam("student");
        var students = component.get("v.students");
        component.set("v.students", students);

You extract the payLoad from the event and then do as you please with it. In this case I simply extracted the student payLoad and pushed it into my student array.

So this is how an application event is registered, fired and handled! In my next post I will show how to fire component events.

Bishwambhar Sen
Bishwambhar Sen is an IT professional with over 10 years of industry experience. He is a Salesforce certified developer and admin. When he is not configuring and customising, he loves photography, traveling and blogging.
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Share This