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
Nag Raj 32Nag Raj 32 

how to display account and their contacts using datatable in VF page

Hi, 
I am new to Salesforce, I need to display Account and their contacts in single datatable in vf page, formate as shown below.User-added image

please help me in this,

thanks@Regards,
Raj
PawanKumarPawanKumar
Please use below code to achieve your requirements.

----------------------
Visualforce
----------------------
<apex:page standardController="Opportunity" sidebar="false" doctype="html-5.0" extensions="OpportunityContacts">
    <apex:pageBlock title="Main Contact Info">
        <apex:pageBlockSection >
            <apex:dataTable value="{!oppAllContacts}" var="eachContact">
                <apex:column headerValue="Contact First Name" value="{!eachContact.FirstName}"/>
                <apex:column headerValue="Contact Last Name" value="{!eachContact.LastName}"/>
                <apex:column headerValue="Preferred Contact Time" value="{!eachContact.Preferred_Contact_Time__c}"/>
                <apex:column headerValue="Inquiring For" value="{!eachContact.Inquiring_For__c}"/>
                <!-- add your other columns here -->
            </apex:dataTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

------------------------
Extension Controller
------------------------
public class OpportunityContacts {
  private final Id oppId;
    public List<Contact> oppAllContacts {get;set;}
    public OpportunityContacts(ApexPages.StandardController stdController) {
        this.oppId = ((Opportunity)stdController.getRecord()).Id;
        Opportunity opp = [select id,accountid from opportunity where id = :oppId];
        if (opp.AccountId != null) {
            oppAllContacts = [select firstname,lastname,AccountId,Preferred_Contact_Time__c,Inquiring_For__c from contact where accountid = :opp.AccountId];
        }
    }
}

Regards,
Pawan Kumar
PawanKumarPawanKumar
Please let me know if it helps you.

Regards,
Pawan Kumar