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
Nihar Annamaneni 7Nihar Annamaneni 7 

Create a VF page to display Account and corresponding contact records using apex class.Write parent to child soql query.

Best Answer chosen by Nihar Annamaneni 7
mukesh guptamukesh gupta
Hi Nihar,

you can use below code for your requirment:-

VF page:-
 
<apex:page controller="contactLists">
    <apex:pageBlock >
        <apex:repeat value="{!Accounts}" var="acc">
            <apex:pageBlockSection title="{!acc.name}"></apex:pageBlockSection>
            <apex:pageBlockTable value="{!acc.Contacts}" var="cont">
                <apex:column value="{!cont.FirstName}"/>
                <apex:column value="{!cont.LastName}"/>
            </apex:pageBlockTable>
        </apex:repeat>      
    </apex:pageBlock>
</apex:page>
public class contactLists {

        public List<Account> getAccounts(){
        return [select id,name,(select id, FirstName,LastName from Contacts) from Account where id in (select accountid from contact where accountid!=null )];
    }

}

if you have any issue , please let me know!!


Kindly mark my solution as the best answer if it helps you.

Regards
Mukesh

All Answers

Sunil MadanaSunil Madana
Hi Nihar,
You might want to refer the below link which has what you want.

http://www.infallibletechie.com/2012/11/tree-view-in-visualforce-page.html

Hope the above answer helps and please mark as correct answer so others can benefit from it.
mukesh guptamukesh gupta
Hi Nihar,

you can use below code for your requirment:-

VF page:-
 
<apex:page controller="contactLists">
    <apex:pageBlock >
        <apex:repeat value="{!Accounts}" var="acc">
            <apex:pageBlockSection title="{!acc.name}"></apex:pageBlockSection>
            <apex:pageBlockTable value="{!acc.Contacts}" var="cont">
                <apex:column value="{!cont.FirstName}"/>
                <apex:column value="{!cont.LastName}"/>
            </apex:pageBlockTable>
        </apex:repeat>      
    </apex:pageBlock>
</apex:page>
public class contactLists {

        public List<Account> getAccounts(){
        return [select id,name,(select id, FirstName,LastName from Contacts) from Account where id in (select accountid from contact where accountid!=null )];
    }

}

if you have any issue , please let me know!!


Kindly mark my solution as the best answer if it helps you.

Regards
Mukesh
This was selected as the best answer