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
Ramana123Ramana123 

how to display account related contacts using pageblock and apex controller class.can any one give me the code plz.?

Best Answer chosen by Ramana123
Foram Rana RForam Rana R
Display All Account and it's Contacts

VFP
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock >
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>

Controller :
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

 

All Answers

Raghu NaniRaghu Nani
Hi Srikanth,

Please have a look into below link and let me know if you needany thing.
https://developer.salesforce.com/forums/?id=906F000000096qGIAQ
Ramana123Ramana123
Tnx Nani for ur help,
The above code is written in apex repeat .but i need it using pageblocktable 
Ramana123Ramana123
can you please modify the above code by using pageblocktable .
Foram Rana RForam Rana R
Hi Srikanth,

Hope you are doing well..!!
Please Try below Code:
<!-- Page: -->

<apex:page standardController="Account">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!account.Contacts}" var="item">

            <apex:column value="{!item.name}"/>

        </apex:pageBlockTable>

    </apex:pageBlock>

</apex:page>

Make Sure After preview Add ?id=0012v00002jrjfdAAA
Here 0012v00002jrjfdAAA is my Account Id

Hope this will help you.
Mark It as best Answer if you resolve it.

Thanks,
Foram Rana
Ramana123Ramana123
hi Rana , Tnx for code.can you give me code without passing Id
Foram Rana RForam Rana R
Display All Account and it's Contacts

VFP
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock >
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>

Controller :
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

 
This was selected as the best answer