You need to sign in to do that
Don't have an account?
rahul tiwari 110
How to display account and i's contact in wrapper class in lightning?
Hi everyone, I m new to lightning and wanted to show accounts and their contacts in a wrapper class by lightning component, whenever an account is clicked it should show its related contacts under the account name.
can anyone pls help me with it
Thanks
can anyone pls help me with it
Thanks
I have solved the issue and it is working fine.
Declare this attribute on your component:-
<aura:attribute name="conWrapperList" type="object"/>
Now, on helperGetDetail method set :- component.set('v.conWrapperList',response.getReturnValue());
Now on iteration of contacts change with this code :-
<lightning:select label="contact list" >
<aura:iteration items="{!v.conWrapperList.conList}" var="con">
<option label="{!con.Name}" value="{!con.Id}"/>
</aura:iteration>
</lightning:select>
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
All Answers
Please follow the code:-
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
thanks for the solution, but i m facing an error in class it is giving that contactList variable does not exist while it has been given in getContact method, I tried to wrote it in fetchaccount method but got the same error .
can u pls help me?
Thank sin advance
Are you getting data from the apex class? Debug each step to know in which line and class you are getting errors. If you are getting data from apex class then in aura component change this:-
// declare this attribute
<aura:attribute name="wrapperList" type="object"/>
While iterating account
getAllAccount helper = component.set('v.wrapperList',response.getReturnValue());
<aura:iteration items="{!v.wrapperList.accList}" var="acc">
same for contact List:-
in helperGetDetail helper = component.set('v.wrapperList',response.getReturnValue());
<aura:iteration items="{!v.wrapperList.conList}" var="con">
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
At the top of your code update contact List with:-
@Auraenabled
public static List<contact> contactList{get; set;}
and in getContact method inside catch change this:-
catch(exception e){
System.debug('error--> '+e.getMessage()+' at ling no--> '+e.getLineNumber());
wrapperInstance.conList = contactList;
wrapperInstance.message = e.getMessage();
wrapperInstance.isSuccess = false;
}
after changing this plz apply the changes that I have mentioned in my previous reply.
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
helper getallaccount & getdetail
bust still not able to save the class
In line 52 pls change :- wrapperInstance.conList = contactList;
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
@Auraenabled
public static List<account> accountList{get; set;}
@Auraenabled
public static List<contact> contactList{get; set;}
@Auraenabled
public static WrapperAccount wrapperInstance{get; set;}
@Auraenabled
public static WrapperAccount fetchAccount(){
wrapperInstance = new WrapperAccount();
accountList = new List<account>();
try{
for(Account accountInstance: [Select Id,Name,Phone from Account WITH SECURITY_ENFORCED]){
accountList.add(accountInstance);
system.debug('accountList@@@'+accountList);
}
wrapperInstance.accList = accountList;
system.debug('accListtry=='+wrapperInstance.accList);
wrapperInstance.message = 'No Error';
wrapperInstance.isSuccess = true;
}
catch(exception e){
System.debug('error--> '+e.getMessage()+' at ling no--> '+e.getLineNumber());
wrapperInstance.accList = accountList;
system.debug('accListcatch=='+wrapperInstance.accList);
wrapperInstance.message = e.getMessage();
wrapperInstance.isSuccess = false;
}
return wrapperInstance;
}
@Auraenabled
public static WrapperAccount getContact(id accid){
wrapperInstance = new WrapperAccount();
contactList = new List<contact>();
try{
for(Contact accountInstance: [Select Id,Name,Phone from contact where accountid =:accid]){
contactList.add(accountInstance);
system.debug('contactList=='+contactList);
}
wrapperInstance.conList = contactList;
system.debug('conListtry='+wrapperInstance.conList);
wrapperInstance.message = 'No Error';
wrapperInstance.isSuccess = true;
}
catch(exception e){
System.debug('error--- '+e.getMessage()+' at line no=== '+e.getLineNumber());
wrapperInstance.conList = contactList;
system.debug('conListcatch=='+wrapperInstance.conList);
wrapperInstance.message = e.getMessage();
wrapperInstance.isSuccess = false;
}
return wrapperInstance;
}
public class WrapperAccount{
@Auraenabled
public List<Account> accList{get; set;}
@Auraenabled
public List<contact> conList{get; set;}
@Auraenabled
public Boolean isSuccess{get; set;}
@Auraenabled
public String message{get; set;}
}
}
I've check edall debugs,accounts debug are not showing in debug logs but contact's debug are coming
ContactList and conList of wrapper are getting all related contacts of it's account but on page no records are shown
I have solved the issue and it is working fine.
Declare this attribute on your component:-
<aura:attribute name="conWrapperList" type="object"/>
Now, on helperGetDetail method set :- component.set('v.conWrapperList',response.getReturnValue());
Now on iteration of contacts change with this code :-
<lightning:select label="contact list" >
<aura:iteration items="{!v.conWrapperList.conList}" var="con">
<option label="{!con.Name}" value="{!con.Id}"/>
</aura:iteration>
</lightning:select>
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi