A Simple Example of Application Event Handling Between two Custom Lightning Components in Salesforce

Vijay Kumar
2 min readSep 2, 2020

Source Page:- w3web.net

Search About India :- surfmyindia

Real time scenarios:- Create an event in Salesforce lightning, and Checked the Value of Checkbox from parent component to child component on click button in Lightning Component using Application event.

Files we used in this post example:-

eventApp1.appLightning ApplicationIt is used for call the component to preview on browser.

eventRegisterCmp1.cmp

Lightning ComponentIt is a parent component and hold a click button.

eventRegisterCmp1Controller.js

JavaScript Controller FileIt is used for click function for Checkbox Checked the Value.eventHandlerCmp1.cmpLightning ComponentIt is a child component of eventRegisterCmp1.cmp, It is hold checkbox input.eventHandlerCmp1Controller.jsJavaScript Controller FileIt is fire automatically and get the value of event from parent component and set value of attribute.repCheckboxEvent.evtLightning EventCreat a application event, hold a boolean type attribute that default value is false.

Live Demo

Step 1:- Create Lightning Application : eventApp1.app

From Developer Console >> File >> New >> Lightning Application

eventApp1.app [Component Application File]

<aura:application extends=”force:slds”>

<c:eventRegisterCmp1/>

</aura:application>

Step 2:- Create Lightning Component : eventRegisterCmp1.cmp

From Developer Console >> File >> New >> Lightning Component

eventRegisterCmp1.cmp [Lightning Component File]

<aura:component >

<aura:registerEvent name=”repCheck” type=”c:repCheckboxEvent”/>

<div class=”slds slds-p-around — medium”>

<button class=”slds-button slds-button — brand” onclick=”{!c.checkVal}”>

checkValue

</button>

<br/><br/>

<c:eventHandlerCmp1/>

</div>

</aura:component>

Step 3:- Create Lightning Component : eventRegisterCmp1Controller.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

eventRegisterCmp1Controller.js [JavaScript Controller]

({

checkVal : function(component, event, helper) {

var repCheck = $A.get(“e.c:repCheckboxEvent”);

repCheck.setParams({

“selfCheckValue”:true

});

repCheck.fire();

}

})

Step 4:- Create Lightning Component : eventHandlerCmp1.cmp

From Developer Console >> File >> New >> Lightning Component

eventHandlerCmp1.cmp [Lightning Component File]

<aura:component>

<aura:handler event=”c:repCheckboxEvent” action=”{!c.repCheckEnable}”/>

<aura:attribute name=”selfChildDeal” type=”Boolean”/>

<div class=”slds slda-p-around — medium”>

<div class=”slds-form-element__control”>

<label class=”slds-checkbox”>

<ui:inputCheckbox aura:id=”repCheckId” value=”{!v.selfChildDeal}” />

<span class=”slds-checkbox — faux”></span>

<span class=”slds-form-element__label slds-m-left — x-small” style=”font-size: 0.75rem;”>

Self Representative

</span>

</label>

</div>

<br/>

</div>

</aura:component>

Step 5:- Create Lightning Component : eventHandlerCmp1Controller.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

eventHandlerCmp1Controller.js [JavaScript Controller]

({

repCheckEnable : function(component, event, helper) {

var selfCheckValue1 = event.getParam(“selfCheckValue”);

component.set(“v.selfChildDeal”, selfCheckValue1);

//alert(‘selfCheckValue ‘ + selfCheckValue);

}

})

Step 6:- Lightning Event : repCheckboxEvent.evt

From Developer Console >> File >> New >> Lightning Event

repCheckboxEvent.evt [Lightning Component File]

<aura:event type=”Application” description=”Event template” access=”global”>

<aura:attribute name=”selfCheckValue” type=”Boolean” default=”false”/>

</aura:event>

Source Page:- w3web.net

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response