You need to sign in to do that
Don't have an account?
Deepak Todkar
My requirement is to show account in col 1 and its related contacts in col 2 row by row. I am trying to achieve this using wrapper class.
Please let me know how to display it on my aura component as below is m code.
component
-----------------------------
<aura:component controller='AccountContact'>
<aura:attribute name='accConList' type='Object[]'/>
<aura:handler name='init' value='{!this}' action='{!c.doInit}'/>
-------- {!v.accConList[0].accRec[0].Name}
<aura:iteration items='{!v.accConList}' var='accCon'>
lets see !!!!
{!accCon.accRec[0].name}
</aura:iteration>
</aura:component>
---------------------
controller.js
-----------------------------------------------------
({
doInit : function(component, event, helper) {
//alert('inside do init');
var action=component.get('c.fetchAccountContact');
action.setCallback(this,function(response){
var state=response.getState();
if(state=='SUCCESS' || state=='DRAFT'){
var returnValue=response.getReturnValue();
console.log('data returned---'+returnValue);
console.log('data returned---'+JSON.stringify(returnValue));
alert('reurt value---'+JSON.stringify(returnValue));
component.set('v.accConList',response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
--------------------------------
apex controller
-----------------------------------------
public class AccountContact{
public static list accList=new list();
public static List accConList{get; set;}
@AuraEnabled
public static List fetchAccountContact()
{
accList=[Select Id,Name,Phone,(select id,lastname from contacts) from Account];
for(integer i=0;i();
accConList.add(new AccConWrap(accList[i],accList[i].Contacts));
}
return accConList;
}
public class AccConWrap{
public Account accRec{get;set;}
public List conRecList{get;set;}
public AccConWrap(Account acc,List con){
accRec=acc;
conRecList=con;
}
}
}
------------------------------------------------
component
-----------------------------
<aura:component controller='AccountContact'>
<aura:attribute name='accConList' type='Object[]'/>
<aura:handler name='init' value='{!this}' action='{!c.doInit}'/>
-------- {!v.accConList[0].accRec[0].Name}
<aura:iteration items='{!v.accConList}' var='accCon'>
lets see !!!!
{!accCon.accRec[0].name}
</aura:iteration>
</aura:component>
---------------------
controller.js
-----------------------------------------------------
({
doInit : function(component, event, helper) {
//alert('inside do init');
var action=component.get('c.fetchAccountContact');
action.setCallback(this,function(response){
var state=response.getState();
if(state=='SUCCESS' || state=='DRAFT'){
var returnValue=response.getReturnValue();
console.log('data returned---'+returnValue);
console.log('data returned---'+JSON.stringify(returnValue));
alert('reurt value---'+JSON.stringify(returnValue));
component.set('v.accConList',response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
--------------------------------
apex controller
-----------------------------------------
public class AccountContact{
public static list accList=new list();
public static List accConList{get; set;}
@AuraEnabled
public static List fetchAccountContact()
{
accList=[Select Id,Name,Phone,(select id,lastname from contacts) from Account];
for(integer i=0;i();
accConList.add(new AccConWrap(accList[i],accList[i].Contacts));
}
return accConList;
}
public class AccConWrap{
public Account accRec{get;set;}
public List conRecList{get;set;}
public AccConWrap(Account acc,List con){
accRec=acc;
conRecList=con;
}
}
}
------------------------------------------------
Generally {Object Object} error goes off after using JSON.stringify but not sure what is wrong but I tried checking and found below link that seems to show accounts and related contacts as a tree structure can you please have a look.
>>https://www.salesforcehut.com/2019/08/salesforce-lightning-tree-structure-for.html
Regards,
Anutej
All Answers
Are you facing any issue while implementing the scenario??
Regards,
Anutej.
yes.
I am getting the accounts and related contacts in the accConList
Also, I am able to set these values to my attribute 'accConList' on my component.
But not able to access those or view those in tabular form.
When i print {!accCon} inside itearation block, its printing '{Object object}'.
I want to have Account Name and Contact names instead of this.
Thanks in advance,
Deepak
Generally {Object Object} error goes off after using JSON.stringify but not sure what is wrong but I tried checking and found below link that seems to show accounts and related contacts as a tree structure can you please have a look.
>>https://www.salesforcehut.com/2019/08/salesforce-lightning-tree-structure-for.html
Regards,
Anutej