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
Pramodh_ShivaPramodh_Shiva 

Parent Component to Child Component event

Hi,
Am new to lightning and got stuck up with a small issue.
I have parent component 'TaskModel' which I will be invoking from quick action and will get record ID. I have child (inner component) which also requires record ID, hasRecordId didn't work in child component so I was trying to pass value from parent component using event. I tried all poosible ways (APPLICATION/COMPONENT event) still couldn't find out why child component event method is not executing.
<!--passAccountId.evt-->

<aura:event type="APPLICATION" description="Event template" >
<aura:attribute name="AccountId" type="String"/>
</aura:event>

<!--TaskModel.cmp--> 
<aura:component controller="AccountStatforStatus" implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
<aura:registerEvent name="navAccountId" type="c:passAccountId"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="showAccount" type="Boolean"/>
<aura:attribute name="recordId" type="Id"/>
<aura:if isTrue="{!v.showAccount}">
<c:accountProspectSuspectUpdate/>
</aura:if>
</aura:component>

<!--TaskModelController.js-->
({
    doInit : function(component, event, helper) {
        
        var evt = $A.get("e.c:passAccountId");
        alert('TM AID '+component.get("v.recordId"));
        var evtAid = component.get("v.recordId");
        evt.setParams({"AccountId":evtAid.toString()});
        evt.fire();
        alert('event fired');
        /*var compEvent = component.getEvent("navAccountId");
        alert('TM AID '+component.get("v.recordId"));
        var evtAid = component.get("v.recordId");
        compEvent.setParams({"AccountId":evtAid});
        compEvent.fire();*/
        var action = component.get("c.getAccountStat");
action.setParams({
"accountId": component.get("v.recordId")
});
// Register the callback function
action.setCallback(this, function(response) {
            var data = response.getReturnValue();
            component.set("v.showAccount",data);
            if(!data){
                alert('Not Fire');
                var createRecordEvent = $A.get("e.force:createRecord");
                createRecordEvent.setParams({
                    "entityApiName": "Task"
                });
                createRecordEvent.fire();
            }
        });
        $A.enqueueAction(action);
    }
})

<!--accountProspectSuspectUpdate.cmp-->
<aura:component controller="AccountStatforStatus" implements="force:appHostable,flexipage:availableForAllPageTypes">
<aura:handler event="c:passAccountId" action="{!c.ValueFromApplicationEvent}" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="accountRecordId" type="Id" />
<aura:attribute name="selectedVal" type="String" />
<aura:attribute name="picklistValues" type="Object" />
<lightning:select value="{!v.selectedVal}" name="selectSP" aura:id="selectSP" label="Suspect/Prospect" required="true" >
<option value="" >--Choose One--</option>
<aura:iteration items="{!v.picklistValues}" var="sp">
<option value="{!sp}" text="{!sp}"></option>
</aura:iteration>
</lightning:select>
<lightning:buttonGroup>
<lightning:button variant="brand" label="Update" onclick="{! c.updateAccount }"/>
<lightning:button label="Cancel" />
</lightning:buttonGroup>
</aura:component>

<!--controller-->
({
doInit : function(component, event, helper) {
        var action = component.get("c.getPickListValuesIntoList");
action.setCallback(this, function(response) {
var list = response.getReturnValue();
component.set("v.picklistValues", list);
})
// Invoke the service
        $A.enqueueAction(action);
},
ValueFromApplicationEvent : function(component,event,helper){
var evtAid = event.getParam("AccountId");
alert('Event '+evtAid);
component.set("v.accountRecordId",evtAid);
},
updateAccount : function(component, event, helper) {
var action = component.get("c.updateAccountStat");
alert('Record Id'+component.get("v.accountRecordId"));
action.setParams({
"accountId": component.get("v.accountRecordId"),
"accountStat" : component.find("selectSP").get("v.value")
});
// Register the callback function
action.setCallback(this, function(response) {
var updateResult = response.getReturnValue();
if(updateResult === 'Success'){
alert('Success '+updateResult);
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
"entityApiName": "Task"
});
createRecordEvent.fire();
}
else{
alert('Error '+updateResult);
}
});
$A.enqueueAction(action);
}
})

am not getting value to accountRecordId in child.

Please Help, Thanks in advance.
 
Best Answer chosen by Pramodh_Shiva
Meghna Vijay 7Meghna Vijay 7
Hi Pramod_Shiva,

There is no need to call an event in case sending recordId from Parent To child Component. You can use the following ways:-
1) Aura Method 
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_tag_method.htm
2) Bound Expression
<Parent Component implements="force:hasRecordId">
<c:ChildComponent recordId = "{!v.recordId}"/>
</Parent Component>

Hope it helps, if it does mark it as solved.

Thanks
 

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi Pramod_Shiva,

There is no need to call an event in case sending recordId from Parent To child Component. You can use the following ways:-
1) Aura Method 
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_tag_method.htm
2) Bound Expression
<Parent Component implements="force:hasRecordId">
<c:ChildComponent recordId = "{!v.recordId}"/>
</Parent Component>

Hope it helps, if it does mark it as solved.

Thanks
 
This was selected as the best answer
Pramodh_ShivaPramodh_Shiva
Thank you,
Also can you help me how to close these component when I fire task creation component using $A.get("e.force:createRecord");
Thanks in advance.
suneel varmasuneel varma
Thanks Meghna Vijay 7..
2) Bound Expression is working on my case.