I have created a page which displays records of account tab.the page that displays a list of record is pasted below. what i want to implement is ..where i click a record,it should diplays the detail of record and this shold be done through apex.I have achieved this functionaloty but i am not sure how to fetch the related contatc from the SOQL ojct

VF page

<apex:page controller="recordPageCont">  
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockButtons >
       <apex:commandButton value="Edit" action="{!EditAccount}"/>
       <apex:commandButton value="Cancel" action="{!CancelButton}"/>
  <apex:pageBlockSection >
     <apex:outputField value="{!objAccount.Name}"/>
     <apex:outputField value="{!objAccount.AccountNumber} "/>
     <apex:outputField value="{!objAccount.Type} "/>
     <apex:outputField value="{!objAccount.industry} "/>
     <apex:outputField value="{!objAccount.Rating} "/>
     <b>Email</b> {!objAccount.Name}@salesforce.com
     <apex:outputField value="{!objAccount.Rating} "/>

<apex:pageBlock title="Contacts Related With {!objaccount.name}"></apex:pageBlock>
<apex:form >


below is my apex page.

what this pagedoes is fetch the details of the record which is clicked.

The id of the record is in variabla recId What my question is how to display contact on the vf page. some one told me that i have to iterate through the object "objAccount" to get the contact.

Please let me know how to do this

public class recordPageCont {
public Account objAccount{get;set;}
Public string recId{get;set;}

    public recordPageCont ()
         recId = apexpages.currentpage().getparameters().get('id');
        objAccount =[SELECT Name, AccountNumber ,Type , Industry , Rating,(SELECT Name , MobilePhone,Title from Contacts) 
                     FROM account
                     WHERE ID = :recId ];

    Public pagereference Editaccount()
        Pagereference edtaccount = new Pagereference('/apex/editaccount?id='+recid);
        return edtaccount;

    public pagereference cancelButton()
        Pagereference cnclbutton = new Pagereference('/apex/accountdisplay');
        return cnclbutton;

1 Answer 1


You can extract the contact list from your subquery by treating it as the List<Contact> that it is. If the List has data, grab it and assign it to the contact display list.

Contact[] displayContacts {get;set;} //display in the vf page using a repeat

Account[] accts =[SELECT Name, AccountNumber ,Type , Industry , Rating,(SELECT Name , MobilePhone,Title from Contacts) 
                 FROM account
                 WHERE ID = :recId ];

Account acct = accts[0];
//acct.Contacts is a List<Contact>
if (!acct.Contacts.isEmpty()){
    displayContacts = acct.Contacts;
  • Expression must be a list type: Account at line 14 column 20 - this is the error i am getting for at line "accounts acct = accts[0]" Commented May 21, 2015 at 10:05
  • Looks like you have a typo - I'm assigning the result of the query to a list, and extracting the first element as the account eg Account[] accts = [query]; then Account acct = accts[0]; - copy that exactly, don't forget the square brackets. Commented May 21, 2015 at 10:08
  • ur accts acount is equivalent to my objAccount account...right?? which i have declared like public Account objAccount{get;set;} do i need to change to to somethig else???? @CaspNZ Commented May 21, 2015 at 10:15
  • objAccount is a single account, accts is an array - other than that, they are similar. So you could write objAccount = accts[0]; to set your account Commented May 21, 2015 at 10:40
  • The code is not giving any error now...Thanks for ur help..But as i asked..how can i show the fields from display contact on VF page.. coz when i am writing below code..its giving me an error <apex:outputField value="{!displayContacts.Rating} "/> eror is "Error: Unknown property 'recordPageCont.displayContacts" Commented May 21, 2015 at 10:50

You must log in to answer this question.

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