+ Start a Discussion
srikanth yeturu 5srikanth yeturu 5 

2.Display list of accounts in a block with checkbox, if the user selects the accounts and click on "Show Contacts " button the selected account related contacts should display in the pageBlocktable beside the accounts list block

 Right side block should display Account name as a heading and its related contacts under that account make those changes.
 page block 2 is not working ...?

// class

public class AccountSelectClassController {

    public list<wrapaccount> wrapaccountList { get; set; }
    public list<account> selectedAccounts{get;set;}     
    public list<id> selectedrecordsIds{get;set;}
    public list<Contact> records{get;set;}
    public list<Account> names{get;set;}

    public AccountSelectClassController ()
    {
      
       if(wrapaccountList ==null)
            {
                 wrapaccountList =new list<wrapaccount>();
                 for(account a:[select id,name from account limit 10])
                 {
                 wrapaccountlist.add(new wrapaccount(a));
        
                  }
            }
     }
    
     public void ProcessSelected()
     {
       selectedAccounts=new list<account>();
       selectedrecordsIds =new list<id>();
       records =new List<Contact>();
       names =new List<Account>();
       for(wrapaccount wrapobj:wrapaccountlist)
          {
              if(wrapobj.isSelected==true)
                {
                   selectedAccounts.add(wrapobj.accn);
                }   
          }
          
          for(Account abc:selectedAccounts)
          {
            selectedrecordsIds.add(abc.id);   
          }
                names = [SELECT Name,label__c FROM ACCOUNT WHERE Id IN :selectedrecordsIds];
         
             records = [SELECT Name FROM Contact WHERE AccountId IN : selectedrecordsIds];
               
             System.debug('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'+records);

          System.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'+selectedrecordsIds);
            System.debug('ccccccccccccccccccccccccccccccccccccccc'+names);

      }
      
  
   
   public class wrapaccount
   {
    
    public account accn{get;set;}
    public boolean isSelected{get;set;}
     
       public wrapaccount(account a)
       {
     
         accn=a;
         isselected=false;
       }
   }
}


vf pafe :-
<apex:page sidebar="false" controller="AccountSelectClassController">
    
    
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    
    </script>
 <apex:form >
    
     <apex:pageBlock id="block2">
       
       <apex:pageBlockSection columns="2">
         <apex:pageBlockTable value="{!wrapaccountList}" var="waccl">
            
           <apex:column >
              <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
            <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
           </apex:column>
            
            <apex:column value="{!waccl.accn.name}"/>
         </apex:pageBlockTable>
           <apex:pageBlock >
           
   
          <apex:pageBlockTable value="{!names}" var="aaa">
             <apex:column value="{!aaa.Name}" headerValue="name"/>

                                
               
              <apex:pageBlockTable value="{!records}" var="qqq">
                  <apex:column value="{!qqq.name}"/>
              </apex:pageBlockTable>
              </apex:pageBlockTable>

           </apex:pageBlock>
       </apex:pageBlockSection>
         
         <apex:pageBlockButtons location="bottom">
          <apex:commandButton action="{!ProcessSelected}" value="Show Selected Contacts" reRender="block2"/>
        </apex:pageBlockButtons>
         
     </apex:pageBlock>
    
   </apex:form>
</apex:page>


 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Srikanth,
When you want to display pageBlockTable inside other pageblocktable you have to wrap inner table in<apex:column> tag.And you have to rerender 2nd pageblock after clicking show contacts button.
Try this code.Added column tag in 36 line
<apex:page sidebar="false" controller="AccountSelectClassControllers">
    
    
    <script type="text/javascript">
    function selectAllCheckboxes(obj,receivedInputID){
        var inputCheckBox = document.getElementsByTagName("input");
        for(var i=0; i<inputCheckBox.length; i++){
            if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                inputCheckBox[i].checked = obj.checked;
            }
        }
    }
    
    </script>
    <apex:form >
        
        <apex:pageBlock id="block2">
            
            <apex:pageBlockSection columns="2">
                <apex:pageBlockTable value="{!wrapaccountList}" var="waccl">
                    
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
                    </apex:column>
                    
                    <apex:column value="{!waccl.accn.name}"/>
                </apex:pageBlockTable>
                <apex:pageBlock id = "table2" >
                    
                    
                    <apex:pageBlockTable value="{!names}" var="aaa">
                        <apex:column value="{!aaa.Name}" headerValue="name"/>
                        <apex:column breakBefore="true" colspan="2">
                            
                            
                            <apex:pageBlockTable value="{!records}" var="qqq">
                                <apex:column value="{!qqq.name}"/>
                            </apex:pageBlockTable>
                        </apex:column>
                    </apex:pageBlockTable>
                    
                </apex:pageBlock>
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!ProcessSelected}" value="Show Selected Contacts" reRender="table2"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
 
srikanth yeturu 5srikanth yeturu 5
thx chandrika for the code. just look at that picture i need to display only particular account related contacts , but it is showing all contacts for enabled checkboxes . can you help me in this plzz
sunny522sunny522
Hi Srikanth,
you can use 2 separate lists one for displaying account section and other for displaying account and its related contacts.
see the code in https://salesforceglobe4u.blogspot.com/2020/03/how-to-display-account-and-its-related.html

Thanks