You need to sign in to do that
Don't have an account?
Manish Tripathi 20
Hi , I have created a wrapper class to show Account details along with checkbox, when checkbox of particular account checked true , it display contact details but I am getting error.
public class AccountSelectContact{
public List<WrapAccount> wrapAccList{get;set;}
public List<Account> selectedAccounts{get;set;}
public List<Contact> conList{get;set;}
public AccountSelectContact(){
if(wrapAccList== null){
wrapAccList= new List<WrapAccount>();
List<Account> accList=[select id, name,(select id,name,phone from contacts) from Account];
for(Account a: accList){
wrapAccList.add(new WrapAccount(a));
}
}
system.debug('wrapAccList++'+wrapAccList);
}
public void processSelected(){
wrapAccList= new List<WrapAccount>();
selectedAccounts= new List<Account>();
conList=[select id,name,phone from Contact where id IN: accList];
for(WrapAccount w:conList){
if (w.selected==true){
selectedAccounts.add(w.conList);
}
}
}
public class WrapAccount{
public Account acc{get;set;}
public Boolean selected{get;set;}
public WrapAccount(Account a){
this.acc=a;
this.selected=false;
}
}
}
public List<WrapAccount> wrapAccList{get;set;}
public List<Account> selectedAccounts{get;set;}
public List<Contact> conList{get;set;}
public AccountSelectContact(){
if(wrapAccList== null){
wrapAccList= new List<WrapAccount>();
List<Account> accList=[select id, name,(select id,name,phone from contacts) from Account];
for(Account a: accList){
wrapAccList.add(new WrapAccount(a));
}
}
system.debug('wrapAccList++'+wrapAccList);
}
public void processSelected(){
wrapAccList= new List<WrapAccount>();
selectedAccounts= new List<Account>();
conList=[select id,name,phone from Contact where id IN: accList];
for(WrapAccount w:conList){
if (w.selected==true){
selectedAccounts.add(w.conList);
}
}
}
public class WrapAccount{
public Account acc{get;set;}
public Boolean selected{get;set;}
public WrapAccount(Account a){
this.acc=a;
this.selected=false;
}
}
}
All Answers
Hope you are doing great.i've wriiten this code to explain you.
1)_______________________________
2)---------------------------
please see the attached the scope of the list is limited to construtor so try declaring outside and one more thing yo cannot assign valuesof account to contact
please correct your code.
If this answers your question please mark it as best answer.
Warm Regards,
Ram Chand
#StayHomeStaySafe
Your Query helped me and reason are also very much satisfactory.
Thanks
Your explanation is very much helpful for me. But for conList problem is the same.
Your code has a couple of issues,
1. You're defining the accList inside the constructor.
2. The query to get related contacts under any account should be, instead of 3. You're iterating conList against of WrapAccount in the foreach. The loop should be, If you've any other doubt please post them here.
Thank you so much for your support, it was very helpful.
Thanks!
I have written the final code along with the VF page, when I am selecting account checkbox and clicking on Button to get associated Contacts, it is showing error:
Attempt to de-reference a null object
Error is in expression '{!processSelected}' in component <apex:commandButton> in page accountselectcontact: External entry point.
I know about the reason but not able to find the solution for the error. please help, little more.
Apex:
public class AccountSelectContact{
public List<WrapAccount> wrapAccList{get;set;}
public List<Account> selectedAccounts{get;set;}
public List<Account> accList{get;set;}
public List<Contact> conList{get;set;}
public AccountSelectContact(){
if(wrapAccList== null){
wrapAccList= new List<WrapAccount>();
List<Account> accList=[select id,name,(select id,name from contacts) from Account];
for(Account a: accList){
wrapAccList.add(new WrapAccount(a));
}
}
system.debug('wrapAccList++'+wrapAccList);
}
public void processSelected(){
if(wrapAccList !=null){
wrapAccList= new List<WrapAccount>();
selectedAccounts= new List<Account>();
conList=[select id,name from Contact where AccountId IN: accList];
for(WrapAccount w:wrapAccList){
if (w.selected==true){
selectedAccounts.add(w.acc);
}
}
}
}
public class WrapAccount{
public Account acc{get;set;}
public Boolean selected{get;set;}
public WrapAccount(Account a){
this.acc=a;
this.selected=false;
}
}
}
VF Page:
<apex:page controller="AccountSelectContact">
<apex:form>
<apex:pageBlock title="All Accounts">
<apex:pageBlockButtons>
<apex:commandButton action="{!processSelected}" value="Get Contacts" rerender="t2"/>
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:pageBlockTable value="{!wrapAccList}" var="ac">
<apex:column >
<apex:inputCheckbox value="{!ac.selected}"/>
</apex:column>
<apex:column value="{!ac.acc.name}"/>
<apex:column value="{!ac.acc.id}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!selectedAccounts}" var="sl" id="t2">
<apex:column value="{!sl.name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
You haven't initialized conList, please add in the constructor.
Thanks for your valuable time, I initialized conList but the error still remain same
I'm not really sure what you actually want to achieve. I think you wanted to show contacts of the selected contacts.
If that is what you needed, please try with the following code:
The condition which you gave
Firstly I was not using if condition, the same problem was there that time also, then I used this, but now I will work with Try catch also, will let you know about output.
Yes try it if it doesn't work out post it here i'll help you out
When I am hitting the button, every time contact is getting repeated because, every time the constructor is getting executed, and the same contacts ate coming multiple times.
Else everything working fine, and I got all your points that I was missing.
Thanks!!!
Thanks Sachin!!!!!!!!!!!!!!