0

On clicking of a button in child Component I'm passing the "Object and String" type attribute values from Child to Parent using the Component Event. The Problem here is I'm not able to receive the expected data in case of Object type attribute where as in string type attribute I'm able to get it.

Cls :-

public class accAuraController {

@AuraEnabled

    public static cmpEvntWrapper accReleated(string recId){
        system.debug('recId....'+recId);
        cmpEvntWrapper aa = new cmpEvntWrapper();
        if(recId!=null){
            aa.conList = [select name,phone,id,email,Account.Name,CleanStatus,Department,Languages__c from contact where accountId =: recId];
            system.debug('cnList'+aa.conList);
            aa.oppList=[select name,id,AccountId from opportunity where accountId =:recId];
                
        }
        system.debug('wrapData....'+aa);
        return aa;
    }
    
    public class cmpEvntWrapper{
        @AuraEnabled 
        public list<Contact>conList;
        @AuraEnabled 
        public list<opportunity>oppList;
    }
}

<--Child Component-->

<aura:component Controller="accAuraController" >
    
    <aura:attribute name="accLists" type="Object"/>
    <aura:attribute name="recordId" type="string"/>
    <aura:registerEvent name="wrapCls" type="c:cmpEvntTest"/>
    <lightning:button label="sendList" onclick="{!c.fireEvnt}"/>
    
</aura:component>

Js:-

fireEvnt : function(component, event, helper) {
    var action2 = component.get('c.accReleated');
    action2.setParams({
        recId : component.get('v.recordId')
    });
    action2.setCallback(this,function(response){
        if(response.getState() === 'SUCCESS'){
            component.set('v.accLists', response.getReturnValue());
            console.log('**v.accLists2**',component.get('v.accLists'));
            var cmpEvt = component.getEvent('wrapCls');
            cmpEvt.setParams({
                "wrapList"  : response.getReturnValue(),
                wrapText : 'list sent'
            });
            cmpEvt.fire();
            console.log('...',cmpEvt.setParams({
                wrapList  : component.get('v.accLists')
            }));
        }
    },'ALL');
    $A.enqueueAction(action2);
}

ComponentEvent :-

<aura:event type="COMPONENT" description="Event template" >

    <aura:attribute name="wrapList" type="Object" />
    <aura:attribute name="wrapText" type="String" />

</aura:event>

<--ParentComponent-->

<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    <aura:attribute name="wrapperList" type="Object"/>
    <aura:handler name="wrapCls" event="c:cmpEvntTest" action="{!c.handleChild}"/>
    <c:cmpEvntChild recordId ="{!v.recordId}"/>

</aura:component>

JS :-

handleChild : function(component, event, helper) {

    //alert('handled');
    var paramsCmp = event.getParam("wrapList");
    var paramsCmp2 = event.getParam("wrapText");
    console.log('**wrapList**'...,paramsCmp);
    console.log('**wrapText**...',paramsCmp2);
}

enter image description here

1 Answer 1

0

To pass the wrapper class from your child Aura component to your parent Aura component, you need to serialize it using JSON.stringify().

({
    fireEvnt : function(component, event, helper) {
        var action2 = component.get('c.accReleated');
        action2.setParams({
            recId : component.get('v.recordId')
        });
        action2.setCallback(this,function(response){
            if(response.getState() === 'SUCCESS'){
                component.set('v.accLists', response.getReturnValue());
                console.log('**v.accLists2**',component.get('v.accLists'));
                var cmpEvt = component.getEvent('wrapCls');
                var wrapRecords = JSON.stringify(component.get("v.accLists"));
                cmpEvt.setParams({
                    "wrapList"  : wrapRecords,
                    wrapText : 'list sent'
                });
                cmpEvt.fire();
            }
        },'ALL');
        $A.enqueueAction(action2);
    }
})
2
  • Thankyou it's worked ...
    – user111359
    Commented Feb 17, 2022 at 6:48
  • @user111359 If the answer solved your problem, please accept the answer. Questions with accepted answers help the entire community.
    – Saroj Bera
    Commented Feb 17, 2022 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .