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
bhanu challengebhanu challenge 

aany one plzanswer it.....its imporatnt..


account to contact parent to chlid relationship is there.....so i want apex code along with vf page code to display total no of account and contacts in vf page........................? help the code
Vasani ParthVasani Parth
If you're trying to display the total number of contacts on an account detail page you'll need to use Visualforce and an extension

Visualforce Page
<apex:page standardController="account" extensions="contactsOnAccountsExtension"> <apex:outputLabel value="The total number of contacts on Account is: "/> <apex:outputText value="{!sum}"/> </apex:page>
Apex Class
public class contactsOnAccountsExtension{ private final Account acct; public Integer sum {get; set;} public contactsOnAccountsExtension(ApexPages.StandardController stdController) { this.acct = (Account)stdController.getRecord(); sum = [SELECT count() FROM Contact WHERE AccountId =:acct.Id]; } }
Display Visualforce page as part of the account detail page
 
 1. Click Setup.
 2. Click App Setup | Customize | Accounts 
 3. Click Page Layouts | then select the layout.
 4. Select Visualforce pages from the palette and drag the Visualforce page above to an appropriate position on the layout.
 5. Save the layout.

Please mark this as the best answer if this helps
 
NagendraNagendra (Salesforce Developers) 
Hi Bhanu challenge,

Please find the below code snippet matching your requirement.

Visualforce Page :
<apex:page controller="sample1">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!acct}" var="a">
            <apex:column value="{!a.Name}"/>
            <apex:repeat value="{!a.Contacts}" var="c">
                <apex:column value="{!c.Name}"/>
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Page :
public class sample1
{    
    public List<Account> acct {get;set;}
    public sample1()
    {
        acct = [SELECT Name, (SELECT Name FROM Contacts) FROM Account limit 5];
    }

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

Best Regards,
Nagendra.P