+ Start a Discussion
bhanu challengebhanu challenge 

any 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