Is there any way to create report to get the no of contacts on a account.

We can't create roll-up summary fields on account for account.Even if try to create any trigger i got the count from now on ward's.

But what about old contacts.I need exact count of all contacts in an account.


2 Answers 2


First, a trigger can give you the correct totals-- you just need to update all of your records once. For example, you could pull all the records out via Data Loader, then perform a basic update (just the ID value).

However, if you just need a report, you can do that in three easy steps.

  1. Build a Report

In the reports tab, choose Accounts and Contacts for the report type, and in the first filter criteria, use Account Id equals "" (no value). Save this report to a public place, and get your report Id from the URL.

  1. Build a Custom Link

In Account Custom Links, create a new link using the report's URL:


Where pv0 represents the first filter value, and {!Account.Id} is the account's Id that will be merged in.

  1. Add to Page Layout

Edit the Account Page Layout, add the custom link to the layout, and save.

  • I like your answer but it doesn't resolves.After set up everything If i click the custom link in account page it shows for single account in a report but i want it to show all accounts in a single report @sfdcfox
    – Pavan tej
    Apr 26, 2016 at 12:31

I hope this can help you:

I have a @future class that update a list of specified accounts. First, you must create a Number field in you Account Object, then execute the class via trigger o with a button, as you prefer.

public class AccountProcessor {
@future (callout=true)
public static void countContacts(List<Id> accountIds){
    Map<Id, Account> mAccounts = new Map<Id, Account>([SELECT Id, Number_of_Contacts__c FROM Account WHERE id in :accountIds]);
    List<AggregateResult> groupedResults = [SELECT COUNT(Id)myCount, AccountId FROM Contact WHERE Account.Id in : accountIds GROUP BY AccountId];

    for(AggregateResult ar :groupedResults){
        Account myAccount = mAccounts.get(String.valueOf(ar.get('AccountId')));
        myAccount.Number_of_Contacts__c = Integer.valueOf(ar.get('myCount'));
    update mAccounts.values();

If do you need the test code:

public class AccountProcessorTest { 

    private static void testAccountProcessor(){
        Account myAcc = new Account();
        myAcc.Name = 'Test';
        insert myAcc;

        Contact myContact = new Contact();
        myContact.AccountId = myAcc.Id;
        myContact.LastName = 'testName';
        insert myContact;

        List<Id> lIds = new List<Id>();


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .