I want to display the Account of contacts in a table format using a standard controller and a custom extension in a VisualForce page. Can you please give me an idea of how can I solve this problem?

  • One contact can have one account, so how do you want in table format. please clarify your Requirement. Jul 3, 2015 at 5:52
  • one account have number of contacts in table format i need to display the records
    – salesforce
    Jul 3, 2015 at 6:12

3 Answers 3


This controller and page will help you.

public class dataController{
public List<Contact> accLst {get; set;}

public dataController(){

public void searchresult(){
           SYSTEM.DEBUG('Number of records '); 
    accLst = [SELECT Name,lastName,phone,id FROM Contact WHERE name != null LIMIT 999 ];


<apex:pageBlockTable value="{!accLst}" var="ac">
        <apex:column width="25px">
              <apex:column value="{!ac.Name}" headerValue="Name"/>
              <apex:column value="{!ac.LastName}" headerValue="LastName"/>
              <apex:column value="{!ac.phone}" headerValue="Phone"/>
              <apex:column value="{!ac.id}" headerValue="Id"/>


you should build a page like this :

<apex:page standardController="Contact" recordSetVar="contacts" showHeader="true" sidebar="true">
     <apex:pageBlock title="list" id="contacts_list">
         <apex:pageBlockTable title="contacts List" value="{!contacts}" var="c" width="100%">

              <apex:column value="{!c.FirstName}"/>
              <apex:column value="{!c.LastName}"/>
              <apex:column value="{!c.Email}"/>
              <apex:column value="{!c.AccountId}"/>


This will show you list of contacts and their accounts..In case you want to display a different list you should specify your query..

anyhow your extension class should look something like this :

public class ContactExt {

  public Contact currentContact {get; set;}

  public ContactExt(ApexPages.StandardController stdController) {
     Id contactId = stdController.getId();

     currentContact = [Select Id, FirstName,LastName,AccountId From Contact Where Id=:contactId AND AccountId!=null];


This is an example of Contact Extension Class but you should add a list of account according to your needs...

<apex:page controller="FetchRelatedContact">
    <apex:form >
       <apex:pageBlock >
       <apex:outputText value="Select Account    "/>  <apex:selectList value="{!selVal}" size="1">

           <apex:selectOptions value="{!AllAccount}"/>

       <apex:commandButton value="Show" action="{!showRelatedContacts}"/>


       <apex:pageBlockTable var="cont" value="{!contacts}">
           <apex:column value="{!cont.name}"/>
           <apex:column value="{!cont.email}"/>




public class FetchRelatedContact {

    public list<selectoption> AllAccount { get; set; }
    public list<contact> contacts { get; set; }

    public String selVal { get; set; }

    public FetchRelatedContact(){

        Allaccount = new list<selectoption>();

        list<account> acnts = [select id, name from account];

        for(account ac :acnts){

            allaccount.add(new selectoption(ac.id,ac.name));


    public void showRelatedContacts(){

        contacts = [select name,email from contact where accountid = :selVal];


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .