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
sreekanth cheerasreekanth cheera 

Write SOQL to fetch all account records with corresponding contacts and display in VF Page?

Akshay_DhimanAkshay_Dhiman
Hi sreekanth cheera,

  try this code
 
<apex:page controller="RadioButtonController">
    <apex:form >
        <apex:pageBlock title="Account Viewer" >
            <apex:pageBlockSection rendered="{!If(accounts !=null && accounts.size>0,true,false)}" >
                <apex:pageBlockTable value="{!accounts}" var="acc"  width="100%" id="accTable" >
                    <apex:column headerValue="Select">
                        <input type="radio" name="<strong>selectRadio</strong>" id= "radio" >
                        <br/>
                        <apex:actionSupport event="onclick" action="{!getSelected}" status="buttonStatus"  reRender="cntblock" >
                            <apex:param name="accid" value="{!acc.id}" />
                        </apex:actionSupport>
                       </input>
                  </apex:column>
                <apex:column value="{!acc.Name}"/>
                <apex:column value="{!acc.Type}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
       
        <!-- Action status block -->
        <apex:actionStatus id="buttonStatus">
            <apex:facet name="start">
                <apex:outputPanel >
                    <apex:commandButton value="View Contact Records" disabled="true"/>
                </apex:outputPanel>
            </apex:facet>
            <apex:facet name="stop">
                <apex:outputPanel >
                    <apex:commandButton value="View Contact Records" action="{!viewContacts}" reRender="cntblock"/>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionStatus>
    </apex:pageBlock>
   
    <!-- Contact Block -->
    <apex:outputPanel id="cntblock">
        <apex:pageBlock title="Available Contacts" rendered="{!If(contacts != null && contacts.size>0,true,false)}">
            <apex:pageblockSection >
                <apex:pageBlockTable value="{!contacts}" var="con"  width="100%" id="cntTable">
                    <apex:column value="{!con.FirstName}"/>
                    <apex:column value="{!con.LastName}"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageMessage summary="No Contacts Found" severity="Info" strength="3" rendered="{!AND(contacts == null, selectedAccountId != null)}"/>
    </apex:outputPanel>
</apex:form>
 
</apex:page>

public class RadioButtonController
{
    public string selectedAccountId{get; set;}
    public List<Account> accounts{get; set;}
    public List<Contact> contacts{get; set;}
   
    public RadioButtonController()
    {
        accounts = [SELECT Name, Type FROM Account LIMIT 10];
    }
    public PageReference getSelected()
    {
        System.debug('Entered account selection block');
        selectedAccountId = ApexPages.currentPage().getParameters().get('accid');
        contacts = new List<Contact>();
        return null;
    }
    public void viewContacts()
    {  
        if(selectedAccountId != null)
        {
            contacts = [SELECT FirstName, LastName FROM Contact WHERE AccountId =:selectedAccountId];
            if(contacts.size() == 0)
            contacts = null;
        }
    }
}


if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
Amit Chaudhary 8Amit Chaudhary 8
Example 1) You Can try the same without SOQL As well like below
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a" >
                <apex:pageBlockSection title="{!a.name}" columns="1">
                      <apex:relatedList list="Contacts" subject="{!a.Id}" />
                </apex:pageBlockSection>
            </apex:repeat>     
         </apex:pageBlock>
</apex:page>

Example 2) With SOQL please check below post
1) https://developer.salesforce.com/forums/?id=906F0000000g0WzIAI
2) https://developer.salesforce.com/forums/?id=906F000000096qGIAQ

Let us know if this will help you


 
Srikanth Cheera 19Srikanth Cheera 19
Thaks