+ Start a Discussion
Arnold Joseph TodasArnold Joseph Todas 

Display all Account in VF Page

How do i display all Account in VF Page with the Account Name, Type, Address and Created by?

Thanks a lot!
AJ
Best Answer chosen by Arnold Joseph Todas
KaranrajKaranraj

Try the below code for your requirement.

Apex Controller : This controller to get the Account id from the page block table when user click it and it will display the list of contacts associated with that particular contact
public with sharing class DisplayContact {
    public List<Contact> conList {get;set;}
    public DisplayContact(ApexPages.StandardSetController controller) {

    }
    
    public PageReference ContactLists()
    {
    if(ApexPages.currentPage().getParameters().get('id') != null)
      conList = [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')];
     return null;
    }   

}



Visualforce : Visualforce page to display all the acocunt record and its contact record. For the partical page refresh we used rerender concept in the visualforce page
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false" extensions="DisplayContact" >
  <apex:pageBlock title="Account" >
    <apex:form >
      
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">
      
        <apex:column headerValue="Account Name">
        <apex:commandLink rerender="contactDetails" value=" {!a.Name}" action="{!ContactLists}">
         
         <apex:param name="id" value="{!a.id}"/>
       </apex:commandLink> 
         </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
      </apex:pageBlockTable>
     
    </apex:form>
   
  </apex:pageBlock>
  <apex:pageBlock title="Contact">
   <apex:outputPanel id="contactDetails">
     <apex:pageBlockTable value="{!conList}" var="con" id="conlist" title="Contact">
     <apex:column value="{!con.Name}"/>
     <apex:Column value="{!con.Phone}" />
     <apex:Column value="{!con.Email}" />
     </apex:pageBlockTable>
        
  
    </apex:outputPanel>
  </apex:pageBlock>
  
</apex:page>


 

All Answers

KaranrajKaranraj
You can use the standard controller to display the records in your visualforce page. Note Standard controller will run in user mode, if you the user don't have access to the account record then user won't view that record in your visualforce page.

Here is the sample code to display all accounts in the visualforce page
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
  <apex:pageBlock >
    <apex:form >
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">
        <apex:column value="{!a.name}"/>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
      </apex:pageBlockTable>
    </apex:form>
  </apex:pageBlock>
</apex:page>



 
Arnold Joseph TodasArnold Joseph Todas
@Karanraj 
Thanks! but how can i show all the contacts once i clicked an account name on the table?

Thanks for your help!
AJ
Vijay NagarathinamVijay Nagarathinam
Hi Aj,

Try this code, It will be helpful for you.!
 
<apex:page controller="wrapperListAccount">
    <apex:form >
        <apex:outputPanel rendered="{!selectedCheck == false}">
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrappAccList}" var="w" id="test">
                
                <apex:column headerValue="Checkbox">
                    <apex:inputCheckbox value="{!w.checked}" >
                        <apex:actionSupport event="onclick" reRender="test"/>
                    </apex:inputCheckbox>
                </apex:column>
                <apex:column headervalue="Name">
                     <apex:outputtext value="{!w.acc.Name}"/>
                </apex:column>
                <apex:column headerValue="PhoneNumber">
                    <apex:outputField value="{!w.acc.phone}" rendered="{!w.checked}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Display!!" action="{!displayAcc}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        </apex:outputPanel>
        
        <apex:outputPanel >
           <apex:pageBlock rendered="{!selectedCheck == true}">
               <apex:pageBlockTable value="{!conList}" var="cc">
                   <apex:column headerValue="ContactName">
                       <apex:outputfield value="{!cc.lastname}"/>
                   </apex:column>
                   <apex:column headerValue="AccountName"> 
                       <apex:outputfield value="{!cc.accountid}"/>
                   </apex:column>
               </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
 
public class wrapperListAccount 
{
    public list<wrapperAcc> wrappAccList{get;set;}
    public list<Account> getAccList = new list<Account>();
    public list<Contact> conList{get;set;}
    public boolean selectedCheck{get;set;}
    
  public wrapperListAccount()
    {   selectedCheck = false;
        wrappAccList = new list<wrapperAcc>();
        for(Account aa : [SELECT id,name,phone from Account WHERE name != null])
        {
            wrappAccList.add(new wrapperAcc(false,aa));
        }
    }
    
    public pagereference displayAcc()
    {    
        selectedCheck = true;
        conList = new list<Contact>();
        for(wrapperAcc aaa : wrappAccList)
        {
            if(aaa.checked == true)
                getAccList.add(aaa.acc);
              system.debug('@@@@@@@@@@@@@@@'+aaa.acc.name);
        }
        update getAccList;
        for(Contact c : [SELECT id,lastname,accountid,account.name from contact WHERE accountid IN: getAccList])
        {
            c.lastname = c.Account.name +'Contact';
            conList.add(c);
        }
        update conList;
        return null;
    }
    
public class wrapperAcc
{
    public Account acc{get;set;}
    public Boolean checked{get;set;}
    
    Public wrapperAcc(Boolean bool,Account aa)
    {
        acc = aa;
        checked = bool;
    }
}
}

 
Arnold Joseph TodasArnold Joseph Todas
@Vijay Nagarathinam
it should be clickable in AccountName on PageBlockTable here's my code : 
<apex:page sidebar="false" standardController="Account" recordSetVar="accounts" tabstyle="account">
<img src="{!$Resource.OnePiece}" />

<apex:pageBlock title="Pirate Account"> 
    <apex:form >
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">

        <apex:column headerValue="Account Name">
            <apex:outputLink value="/ui/support/servicedesk/ServiceDeskPage?tsid={!a.name}">{!a.name}</apex:outputLink>
        </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
        
      </apex:pageBlockTable>
      <center><apex:commandButton value="Show Contacts"/><apex:commandButton value="Hide Contacts"/></center>
      
    </apex:form>
</apex:pageBlock>


</apex:page>

Thanks a lot!
AJ
KaranrajKaranraj

Try the below code for your requirement.

Apex Controller : This controller to get the Account id from the page block table when user click it and it will display the list of contacts associated with that particular contact
public with sharing class DisplayContact {
    public List<Contact> conList {get;set;}
    public DisplayContact(ApexPages.StandardSetController controller) {

    }
    
    public PageReference ContactLists()
    {
    if(ApexPages.currentPage().getParameters().get('id') != null)
      conList = [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')];
     return null;
    }   

}



Visualforce : Visualforce page to display all the acocunt record and its contact record. For the partical page refresh we used rerender concept in the visualforce page
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false" extensions="DisplayContact" >
  <apex:pageBlock title="Account" >
    <apex:form >
      
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">
      
        <apex:column headerValue="Account Name">
        <apex:commandLink rerender="contactDetails" value=" {!a.Name}" action="{!ContactLists}">
         
         <apex:param name="id" value="{!a.id}"/>
       </apex:commandLink> 
         </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
      </apex:pageBlockTable>
     
    </apex:form>
   
  </apex:pageBlock>
  <apex:pageBlock title="Contact">
   <apex:outputPanel id="contactDetails">
     <apex:pageBlockTable value="{!conList}" var="con" id="conlist" title="Contact">
     <apex:column value="{!con.Name}"/>
     <apex:Column value="{!con.Phone}" />
     <apex:Column value="{!con.Email}" />
     </apex:pageBlockTable>
        
  
    </apex:outputPanel>
  </apex:pageBlock>
  
</apex:page>


 
This was selected as the best answer
Arnold Joseph TodasArnold Joseph Todas
@Karanraj

Wow nice Thanks :)
Kritika KerhalkarKritika Kerhalkar
Hello,

If I have an Object ABC (with attributes name, id, type, status etc.)and the status field has got 3 statuses. How can I show the records of each status, section wise, one below the other.
The requirement doesnt need any command button.

Thanks,
Kritika