SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel
Showing posts with label Salesforce Event. Show all posts
Showing posts with label Salesforce Event. Show all posts

Sunday, 5 August 2018

Salesforce Lightning Events Part 6 - Building Application Events

Welcome to the 6th tutorial of Lightning Events Tutorial Series. Now you know a little about application events and the 3 phases for the propagation of application events namely:- Bubble, Capture and Default phase. If you have joined right now and want to learn application events only then start from my previous blog post here.

So, let's begin development. We'll start by making an application event and I'll be using the previous code from my GitHub Repository here. In this tutorial I am not going to make anything scenario based like we did before (forming the income component)  whereas this time, I am going to simply pass a message using application event and display that.

So, let's begin by making a small change in our previous code but before that I am going to show how our application will finally look like:-


So, this is how our final application will look like, as you can see above I have divided the app in two sections, the upper section contains the component event that we've already done and the lower section consists of application event part that we are going to build right now.

Let's start by making changes in our application. If you just want to learn about the application events and starting from here I'll suggest you to comment the component event part in the below application code i.e. <c:LightningEventsCompContainer /> just enclose it in <!-- --> so that it doesn't cause any errors.

LightningEventsApp

As you can see above, the whole application is divided into two parts. The above part is the component event part and the below part is the application event part. In the application event part, I have called LightningEventsAppContainer component twice and have passed different values (1 and 2) in the id attribute of this component. Now let's move on to LightningEventsAppContainer component and check what's written there.

LightningEventsAppContainer

Above is the code for LightningEventsAppContainer component. As you can see above, I have made two attributes, first is id whose value we're getting from the LightningEventsApp and second is message which we'll display when we'll get the message from the application event. The default value of message is set as none. I have also registered a handler for the event which is referring to LightningApplicationEvent event and also calling the controller function handleApplicationEvent which is responsible for performing the action on capturing application event. I have displayed both the attributes in a div with class outerComponent below in this. After the id and message is displayed, LightningAppComp1 component is called in which I am passing the value of id attribute. Now, let's take a look at the CSS too.

As you can see, I have given the outerComponent class which was applied to the div displaying id, message and containing LightningAppComp1 component too, a black border so that both the instances of LightningEventsAppContainer having id 1 and 2 can be viewed separately from each other. Now, moving on to the controller, let's have a look at the code below:-

As you can see above, we have handleApplicationEvent method which is responsible for handling the application event. In this method, we are getting the value of id from the attribute so that we can show an alert that which instance's handler is called and finally I am setting the message attribute with the value received by application event. In the definition of application event, I have an attribute named message that's why I am using event.getParam("message") here to get the value from my application event.

Now, before moving on to LightningAppComp1, let's have a look at LightningApplicationEvent first.

LightningApplicationEvent

As you can see in the code above, I have used the aura:event tag to define an application event and specified the event type as APPLICATION and I have specified an attribute of type string and having a name - message which I was using in the LightningEventsAppContainer component above. Now, my outer component, application event and app is ready, so it's time to code our source component.

LightningAppComp1

Let's have a look at the below code:-

As you can see, this code is also similar to what we have done in the outer component, we have an attribute named id which is getting the value of id from parent component. As this same inner component is called from two different instances/calls of outer component in app with id 1 and 2. So, this component is getting different id's from both the different instances as each instance have it's own copy of inner component.
Then it consists of registerEvent tag which is referring to LightningApplicationEvent in type and has a name of myAppEvent. Following that, it has an event handler defined, that consists of a reference to LightningApplicationEvent in the event attribute and a controller function handleApplicationEvent is specified in action attribute i.e. this function will be called automatically as there is a need to handle the application event. There is a div having a class of innerComponent that consists of a button having a text Fire Event which will call controller function fireApplicationEvent on click of this button and the application event will fire.

My component is ready now it's time to look at some CSS.

As you can see above, the class of div i.e. innerComponent has a border of red. So, that the inner component can be separated from the outer component. Now, we're going to look at the controller.

As you can see above, in the controller of source component, I have two methods, one is handleApplicationEvent in which we're getting the value of id attribute and displaying it in an alert message so that we know, this handler is from innerComponent which is responsible for firing the event along with the id passed from the outer component instance.
Apart from this, there is fireApplicationEvent method which is called on button click having text Fire Event. To fire the application event, we are getting the reference to it using $A.get("e.c:LightningApplicationEvent"); as we know that the Application Event is global so, here we're getting a reference to event using $A which is itself a global instance name and storing it in a variable named appEvent as we already know that the application event has a single attribute message, So, I am setting that attribute with a value Hello and finally I am firing that event using appEvent.fire(); (Try removing the register event tag and see if the event works - Let me know your result in comments below)

One thing to notice is that right now I have set both the event handlers in inner and outer component to bubble phase as we're going to learn about this phase first. There are two more phases in application events namely:- Application and Default phase. Now, I want you to try this by yourself, fire the application event by clicking the button and see how it behaves, then change the handler phase to application and default and observe the changes.

Tired of reading or just scrolled down..?? Don't worry, you can watch the video too :-)



For the full code used in this tutorial, kindly refer to the applicationeventbubblephase branch of my GitHub repository here
That's all for this post. In the next post, We'll be exploring each phase of Application Event using this demo code and see how the event work in each phase. Till then stay tuned and keep supporting SFDC Stop.

Happy Trailblazing..!!

Tuesday, 24 July 2018

Live Session on Lightning Container Component - Jaipur Developer Fest 2018

Hello Everyone,

I am very happy to share with you all that I'll be speaking at Jaipur Developer Fest 2018. This year, #JDF18 is happening on 28th of July, 2018 | 7:30 A.M onwards. This is an amazing opportunity for all of the amazing salesforce enthusiasts in India to connect with our Salesforce Ohana, learn more and take some of the most valuable insights and learnings with them back to their organization. Below are the details about my session and other activities :-

Embed your JavaScript application in Salesforce using Lightning Container


My session is scheduled from 3:30 P.M - 4:00 P.M. and the title of the session is "Embed your JavaScript application in salesforce using Lightning Container" as stated above. In this session I'll mainly show you a live demo of how you can run any JS application built using 3rd party frameworks like:- React, Angular etc. in your custom Salesforce Lightning Component.

But Wait..!! It doesn't end here. I have one more amazing thing to share.

Circles of Success

This year, the awesome people of JDF announced a new initiative called "Circles of Success". So, now the question arises that :-

What is COS ?

In simple terms, I can say that it is a round table discussion on a particular topic related to Salesforce consisting of 6-10 salesforce trailblazers like you and will consists of 1-2 Salesforce Experts. The best part is that you can come with your doubts regarding any Salesforce Topic for which the COS is scheduled and you can discuss with the expert regarding the same.



I'll be volunteering as an expert in Integration table. So, if you have any doubts regarding apex callouts and integration, postman or any other stuff related to same. You can come to my COS and have a one to one discussion with me. I'll put on my best efforts to help you for sure. The timings of my COS discussion is 1:00 P.M - 1:30 P.M.

For those who are outside and can't attend. My presentation and source code will be available in another blog post after the session for sure. For more information about the full agenda of JDF18 click here.

Hope to meet you at Jaipur Developer Fest very soon.

Happy Trailblazing..!!

Sunday, 20 May 2018

Salesforce Lightning Events Part 5 - Application Events Introduction

Hello and welcome to the 5th tutorial of Salesforce Lightning Events Tutorial Series. As you are already aware of the component events, their types and how they propagate in salesforce. It's time to move on to the application events in Salesforce Lightning.

In simple terms, Application Events are just like broadcast messages. As when you have to send a broadcast message, you broadcast your message from a single device and all the other devices that have the receiver turned on for that message can receive that. Similarly, an application event is fired from one component and all the other components that have the handler defined for that event are notified and they can handle that application event. The component that fires the application event is called Source Component. Lightning Framework supports 3 phases for the propagation of application events:-
  1. Bubble Phase
  2. Capture Phase
  3. Default Phase
Now we'll be discussing these phases one by one:-

Bubble Phase

The flow of event in this phase is much similar to what we have done in the component event. In this phase, the component that fired the event can handle it. The event in bubble phase traverse from the source component to the application root. While the event is traversing towards the application root, any component in the containment hierarchy can handle this event. If any handler stops this event from propagating using event.stopPropagation(), then no more handlers will be called in this phase.

Capture Phase

In this phase, the event handlers are invoked from the application root towards the source component that fired the event. Any registered handler in the component hierarchy can stop the event from propagating by using event.stopPropagation() after that no more handlers will be able to handle the event in this phase or the bubble phase. If you have gone through the previous tutorials, you shoule be aware that the capture phase handlers always run before the bubble phase.

Default Phase

In this phase, the event handlers are invoked from the root to the source component which fired the event. If the event's  propagation wasn't stopped in the previous phase, the root corresponds to the application root. Otherwise, the root corresponds to the component who stopped the event from propagating further using event.stopPropagation(). This phase will execute only when event.preventDefault() was not called previously in capture or bubble phases.

Points to be noted:- 
  1. Any handler in the Bubble or Capture phase can cancel the default behavior of the event by calling event.preventDefault() i.e. if this method is invoked previously, the handlers in the default phase will not handle the event.
  2. If the event's propagation wasn't stopped in the previous phase, the root node for the default phase corresponds to the application root. Otherwise, the root node corresponds to the component in which the handler called event.stopPropagation() to stop the propagation of event.
So, now you know about the basics of application events. In the next post in this series, we'll go into the detail of application events and understand each phase one by one. If you liked this post, do share it among others and let me know your feedback in comments.

Happy Trailblazing..!!

Friday, 11 May 2018

Live Session on Salesforce UI API at TDXGG'18

Hello Everyone,

I am very happy to share with you all that New Delhi Salesforce Developer Group is organizing the TrailheaDx Global Gathering on 12th of May from 12:00 p.m. This is a great learning opportunity for all who reside in and around New Delhi, India. I am also going to give a session on Salesforce User Interface API along with the other speakers including two MVPs and the user group leader as well as a Salesforce MVP himself Mr. Atul Gupta. This event is going to be an awesome place to learn, have fun and connect with the fellow trailblazers. So, mark your calendar and register on the link given below :-

Click Here to Register for the Event


For those who are outside and can't attend. My presentation and source code will be available in another blog post after the session for sure.
Hope to meet you at the event soon.
 

Sunday, 29 April 2018

Salesforce Lightning Events Part 4 - Understanding Container Components

Till now we have learned about component events, bubble and capture phase in component events. Now before moving to the application events, there is one more important concept regarding events which we need to learn and that is Container Components.

What is Container Component ?

Container Components are the components that are not the direct owner/parent of any other component but contain another component between their tags wherever they are defined. For ex:- If I make two lightning components named:- one.cmp and two.cmp and instead of calling two.cmp inside one.cmp I do something like this:-
<c:one.cmp>
                      <c:two.cmp />
</c:one.cmp>
So, in the above case, one.cmp is my container component as it is containing two.cmp within it's  opening and closing tags and is not the parent or direct owner of two.cmp.

While learning about component events bubble and capture phase I have told about hierarchy or containment hierarchy. So, let's see what a container component is and how this hierarchy is implemented. I'll be using the previous code implemented till now so if you just joined or want access to the current implementation you can read my previous blogs in the series listed here. Or at least have a look at the bubble phase implementation here.

So, we have 2 components already implemented:- LightningEventsCompContainer and LightningEventsComp1. Now, I am going to add one more component i.e. LightningEventsCompWrapper. This component is basically a container component which will be wrapping our base component i.e. LightningEventsComp1 and will be a direct child of LightningEventsCompContainer. Let's jump on to the code now for a deeper understanding:-

LightningEventsCompContainer.cmp

We have already used this component as it is the parent of our source component and is also responsible for handling the event fired by the source component. However, I have made a simple change in this component which you can see below:- 
<!-- Wrapper/Parent component called directly from the Lightning Application -->
<aura:component>
    <!-- Attribute to store total income coming through the event -->
    <aura:attribute name="totalIncome" type="decimal" default="0" ></aura:attribute>
    <!-- Handler defined to handle 'totalIncomeComponentEvent' name same as used in registerEvent tag -->
    <aura:handler name="totalIncomeComponentEvent" event="c:LightningComponentEvent"  action="{!c.handleTotalIncomeComponentEvent}"></aura:handler>
    Outer Component
    <!-- Inner component section with border -->
    <div class="innerComponent">
    	Inner Component Section Begin
    	<!-- Calling the inner component container -->
    	<c:LightningEventsCompWrapper>
            <!-- Calling the source component -->
	    <c:LightningEventsComp1 />
	</c:LightningEventsCompWrapper>
	Inner Component Section End
    </div>
    <!-- Section to show total income -->
    <span class="totalIncome">Total Income = {!v.totalIncome}</span>
</aura:component>
As you can see, I have wrapped the LightningEventsComp1 component in another lightning component named LightningEventsCompWrapper i.e. the tags of the wrapper component are now surrounding my source component. In terms of Salesforce, this component which is surrounding the source component in it's tags is called Container Component but in this tutorial, we'll call it as wrapper component for simplicity and as we have already made a component named LightningEventsCompContainer which is containing everything and is the parent component. So, let's move to this wrapper component now and see what we have in it.

LightningEventsCompWrapper.cmp

Below is the code for our new container component for innermost component which is firing the event. As it is wrapping my source component, I have named it as wrapper but in Salesforce docs, such a component is called as container component as it is containing another component within it's tags and is not the direct owner:- 
<!-- Container to wrap inner component -->
<aura:component>
    <!-- Event handler in wrapper component -->
    <aura:handler name="totalIncomeComponentEvent" event="c:LightningComponentEvent"  action="{!c.handleTotalIncomeComponentEvent}" includeFacets="true"></aura:handler>
    <!-- Wrapper Component -->
    <div class="wrapperComponent">
    	Wrapper Component Section Begin
	<!-- Calling the body i.e. the data between the tags of wrapper/container component -->
	{!v.body}
	Wrapper Component Section End
    </div>
</aura:component>
As you can see above, in this component, I have defined a handler which is the same as we have defined in our parent component i.e. LightningEventsCompContainer. This handler is simply responsible for handling the event fired by our source component and is calling the controller function named:- handleTotalIncomeComponentEvent. Below this handler we have defined a div with class wrapperComponent which is responsible for giving a red border to this wrapper component and in the body of the div, I have called {!v.body} which is mainly responsible to get all the data between the tags of this component to this location inside our component as you can see, we have <c:LightningEventsComp1 /> inside the tags <c:LightningEventsCompWrapper> and </c:LightningEventsCompWrapper> in LightningEventsCompContainer.cmp above. So, here {!v.body} will be replaced by <c:LightningEventsComp1 /> and our source component will be inside the Inner Component section Begin and End lines as it takes the position of v.body.

There is one more thing to notice i.e. I have included an extra attribute named includeFacets="true" in the component event handler defined in wrapper component. This is because generally our wrapper component is only containing the source component in it's tags and therefore it'll not be able to handle the event fired by our source component, as it is not the direct owner/parent. So, to make it able to handle the events, we have to include includeFacets="true" attribute in the handler defined in our wrapper component.

LightningEventsCompWrapperController.js

({
    // Function invoked when event is handled
    handleTotalIncomeComponentEvent : function(component, event, helper) {
        alert('Event handler at the wrapper component');
    }
})
As you can see in the above code, I have simply defined a function named handleTotalIncomeComponentEvent which will be called when my event is handled by the wrapper component and it is simply giving an alert with message:- Event handler at the wrapper component so that, we know that the handler at the wrapper component is handling the event.

LightningEventsCompWrapper.css

Below is the css file that is defining the wrapperComponent class used in div to give a red border to our wrapper component in order to differentiate it with other components in the hierarchy.
.THIS {
}
/* Border of wrapper component */
.THIS.wrapperComponent {
    border: 1px solid red;
}
So, we have given the border to our wrapper component using border: 1px solid red; in the wrapperComponent class which is used  by our div.

I have configured all the handlers to work in bubble phase i.e. without having phase="capture" attribute in them and when you run this in your browser and click on Calculate Total Income button, you get the following outputs:-

Event handler at the source component is fired

Event handler at the wrapper component is fired

Event handler at the parent component is fired

Tired of reading or just scrolled down, don't worry, you can watch the video too.


So, in this post we learned about container components and how they are implemented. The whole source code is available in the componenteventwrapper branch of my github repository that you can access directly from here. These container components play a vital role in order to make components generic and will be used in the future tutorials when we'll learn about application events and we'll differentiate it from the component events. If you liked this post, then subscribe to this blog and share among others in your network so that they can get benefit too. Also, make sure to give your feedback in comments.

Happy Trailblazing..!!