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
Manish Tripathi 20Manish 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;
        }
    }
}
Best Answer chosen by Manish Tripathi 20
Sachin HoodaSachin Hooda
And your 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="{!requiredContacts}" var="sl" id="t2">
                <apex:column value="{!sl.name}"/>
            </apex:pageBlockTable>
            
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

All Answers

Manish Tripathi 20Manish Tripathi 20
Here is the error.
Ram Chand HeerekarRam Chand Heerekar
Hi Manish,

Hope you are doing great.i've wriiten this code to explain you.
1)_______________________________
this is before
2)---------------------------
after


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

User-added image

please correct your code.

If this answers your question please mark it as best answer.

Warm Regards,
Ram Chand 
#StayHomeStaySafe
Ram Chand HeerekarRam Chand Heerekar
edit : i've mistakenly attached wrong ss(Last one) in which the query contains account if  in where condition is "id " still it throws error use accountid instead
Manish Tripathi 20Manish Tripathi 20
Hi Sachin,
Your Query helped me and reason are also very much satisfactory.
Thanks
Manish Tripathi 20Manish Tripathi 20
Hi RamChand,

Your explanation is very much helpful for me. But for conList problem is the same.
User-added image
User-added image
Sachin HoodaSachin Hooda
Hi Manish,
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,
conList = [Select Id, Name From Contact Where AccountId IN:accList];
instead of
conList = [Select Id, Name From Contact Where Id IN:accList];
3. You're iterating conList against of WrapAccount in the foreach. The loop should be,
for(WrapAccount w : wrapAccList){
        if (w.selected==true){
              selectedAccounts.add(w.acc);
           }        }
If you've any other doubt please post them here.
Manish Tripathi 20Manish Tripathi 20
Hi Sachin,
Thank you so much for your support, it was very helpful.

Thanks! 
Manish Tripathi 20Manish Tripathi 20
HI 
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>

User-added image
User-added image
Sachin HoodaSachin Hooda
Hi Manish,
You haven't initialized conList, please add
conList = new List<Contact>();
in the constructor.
 
Manish Tripathi 20Manish Tripathi 20
Hi Sachin,

Thanks for your valuable time, I initialized conList but the error still remain same
User-added image
Sachin HoodaSachin Hooda
Hi Manish,
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:
public class AccountSelectContact{
    public List<WrapAccount> wrapAccList{get;set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Contact> requiredContacts{get;set;}
    public List<Account> accList{get;set;}
    public List<Contact> conList{get;set;}
    public AccountSelectContact(){
        if(wrapAccList== null){
            requiredContacts = new List<Contact>();
            wrapAccList= new List<WrapAccount>();
            conList = new List<Contact>();
          	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,AccountId from Contact where AccountId IN: accList];
        for(WrapAccount w:wrapAccList){
            if (w.selected==true){
              selectedAccounts.add(w.acc);
            }
        }
           for(Account acc : selectedAccounts){
               for(Contact con : conList){
                   if(con.AccountId == acc.Id){
                       requiredContacts.add(con);
                   }
               }
           }
       }
    }
    public class WrapAccount{
        public Account acc{get;set;}
        public Boolean selected{get;set;}
        public WrapAccount(Account a){
            this.acc=a;
            this.selected=false;
        }
    }
}
Sachin HoodaSachin Hooda
And your 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="{!requiredContacts}" var="sl" id="t2">
                <apex:column value="{!sl.name}"/>
            </apex:pageBlockTable>
            
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
This was selected as the best answer
Ram Chand HeerekarRam Chand Heerekar
Hi Manish,

The condition which you gave
public void processSelected(){
//this if statement might be getting null since yo haven't handled it in else part try to do a system.debug("try to print the object to know whether it's null or not");
    
   if(wrapAccList !=null){

 
Manish Tripathi 20Manish Tripathi 20
Hi  Ram Chand Heerekar 

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.
Ram Chand HeerekarRam Chand Heerekar
Hi Manish,

Yes try it if it doesn't work out post it here i'll help you out
Manish Tripathi 20Manish Tripathi 20
Hi Sachin

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.
User-added image

Thanks!!!
Sachin HoodaSachin Hooda
Put ,
requiredContacts = new List<Contact>();
outside if condition. This will initialize the list everytime a change to selected accounts is made.
Manish Tripathi 20Manish Tripathi 20
I putted, requiredContacts = new List<Contact>(); outsitde if User-added imagecondition , again it is repeating.
Sachin HoodaSachin Hooda
Please update your processSelected Method,
public void processSelected(){
        requiredContacts = new List<Contact>();
       if(wrapAccList !=null){
        //wrapAccList= new List<WrapAccount>();
        selectedAccounts= new List<Account>();
        conList=[select id,name,AccountId from Contact where AccountId IN: accList];
           System.debug(conList);
           System.debug(wrapAccList.size());
        for(WrapAccount w:wrapAccList){
            if (w.selected==true){
              selectedAccounts.add(w.acc);
            }
        }
           for(Account acc : selectedAccounts){
               for(Contact con : conList){
                   if(con.AccountId == acc.Id){
                       requiredContacts.add(con);
                   }
               }
           }
           System.debug(selectedAccounts);
       }
    }

 
Manish Tripathi 20Manish Tripathi 20

Thanks Sachin!!!!!!!!!!!!!!