Number of Contacts on Account

It would be extremely awful situation for Salesforce Administrators when your the CEO / CTO asks “How many contacts are there on ‘X’ Accounts. This would become a nightmare in Salesforce to achieve this requirement because Roll Up summary only works if you’ve Master – Detail relationship. However it is extremely important in monthly reports on how many accounts we have gained so that we can analysis our success.

Let’s see how can you achieve this requirement. Create the field on Account with the Name – “Number of Contacts (API Name -Account.Number_of_Contacts__c)

Let’s create a small trigger to achieve the above requirement on Contacts –

/* Provide summary of Number of Contacts on Account record */

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
cons =;

// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id, AccountId from Contact where AccountId in :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id, Number_of_Contacts__c from Account where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contact con : contactsForAccounts.values()) {
if (con.AccountId == acct.Id)
if (acct.Number_of_Contacts__c != conIds.size())
acct.Number_of_Contacts__c = conIds.size();

update acctsToUpdate.values();


Test Class for the above Trigger: ContactSumTrigger

* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
* See the Apex Language Reference for more information about Testing and Code Coverage.
private class ContactSumTriggerTest {

static testMethod void contactSumTest() {
//list<Account> acct = [select Id ,Number_of_Contacts__c from Account LIMIT 1];

Account acc = new Account();
acc.Name = ‘test’;

insert acc;

Contact tstCon = new Contact ( LastName = ‘Test’,
AccountId =
insert tstCon;

tstCon.FirstName = ‘Test’;

update tstCon;

delete tstCon;

undelete tstCon;

Next Step – Deploy the code to production. In case your production has data then update all the contacts once and you’re good to go.


About smattey

Sumit Mattey is the Leader, Cloud Technologist & Evangelist, Husband, Father and Fun Loving person. Currently working as - PROJECT MANAGER, R Systems Int'l Ltd.(Salesforce) Not simply defined by his career, Sumit lives with his family (whom he loves so much). He is an avid movie buff, a big book worm, a lover of food and beverage. While his schedule has not been forgiving as he'd like, he also enjoys doing photography with great outdoors and tries to hunt as often as possible.
This entry was posted in Apex, Apex Classes, Code Coverage,, Triggers and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s