Displaying Total Number of Contacts for each Account in Salesforce

If you want to display the “Total Number of Contacts” for each “Account” in Salesforce on the Account page you can not use the Roll Up Summary field. The Roll Up Summary fields on contact are not supported on Accounts. We have used a Trigger on the contact object to  find the sum of the number of contacts which are there in an Account.

The trigger basically updates the Account custom field “Number_of_contacts__c” with the number of contacts that Account has. Since this trigger is on the Standard object it can be used in any Salesforce.com ORG by creating this custom field on Account.

The code below is developed based on Design pattern – ‘Factory Pattern’. For more information about this design pattern please refer this link.

This code has been modified recently

It works on delete of a contact and it also works even if you change the Account of a contact where it updates both the Accounts with the number of contacts.

Note: This code has been modified recently – please share your feedback. The previous code which had several issues is still hosted here.

Comments

  1. Hi!
    Thank you I found this very helpful.
    Regards
    Neil

  2. This is great, are you able to do one where it counts # of contacts in account where contact is set to true for field ‘primary’?

  3. Hi! I’m getting below error for this code:

    Compile Error: Illegal assignment from Integer to String at line 12 column 8

    do you know how to fix this?

    • Surya Kiran says:

      Hello Anna,

      Size method will return integer value. Check you custom field “No_of_Contacts_in_SFDC__c” which must be data type of Integer.

  4. Jeff Langston says:

    Could this trigger be modified to perform a count of contacts that had a specific checkbox field checked?

  5. woww its work.. this code is really helpful.. thank you 😀

  6. Michael says:

    Sorry for the non-dev question. I don’t have a custom field for primary contact (opp.Primary_Contact__c). Is there a need for this field? Or can the apex be altered to not require this field? Thanks!

  7. Skender says:

    Hi,

    I found this very interesting…
    But, could you explain what is the field Primary_Contact__c?

    Thanks,
    Skender

  8. Nishanth H says:

    This won’t work for bulk insertion or updation of Contact records.
    con.size always return a single value. So this logic fails for bulk processing of data.

    • Agree.. If aid has only one account, then this logic works. If aid has more than 1 Account then contact size associate to all Accounts combined in aid will be assigned.

  9. yes, it will not work for bulk records

  10. Great job.

  11. DEVANSHU SOOD says:

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

    Set AccountIds = new Set();

    if(!Trigger.isDelete) {

    for (Contact cont: Trigger.new) {

    if(Trigger.isInsert && cont.AccountId != null){
    AccountIds.add(cont.AccountId);
    }

    if(Trigger.isUpdate) {

    if(cont.AccountId == null && Trigger.oldMap.get(cont.Id).AccountId != null){
    AccountIds.add(Trigger.oldMap.get(cont.Id).AccountId);
    }
    if(cont.AccountId !=null && Trigger.oldMap.get(cont.Id).AccountId != null && cont.AccountId != Trigger.oldMap.get(cont.Id).AccountId){
    AccountIds.add(cont.AccountId);
    AccountIds.add(Trigger.oldMap.get(cont.Id).AccountId);
    }
    if(cont.AccountId !=null && Trigger.oldMap.get(cont.Id).AccountId == null){
    AccountIds.add(cont.AccountId);
    }
    }

    if(Trigger.isUndelete && cont.AccountId != null){
    AccountIds.add(cont.AccountId);
    }
    }
    } else {
    for (Contact ct : Trigger.old){
    if(Trigger.isDelete && ct.AccountId != null){
    AccountIds.add(ct.AccountId);
    }
    }
    }

    List AcctToUpdate = new List();

    for (AggregateResult ar: [Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId]){
    Account tmp = new Account(Id=(Id)ar.get(‘AccountId’), NumberContacts__c=(Decimal)ar.get(‘ContactCount’));
    AcctToUpdate.add(tmp);
    }
    if(AcctToUpdate.size()>0) {
    update AcctToUpdate;
    }
    }

  12. I would also have the question where Primary_Contact__c comes from. I assume it’s a custom field? But on what object? No real insight on programming here. Sorry 😉

  13. Hello, This is the first trigger I’ve ever implemented. I did it in a trailhead playground. Next I’m going to try in a sandbox of my own org. In the playground it seems to work only after a contact is added or deleted in an account. Is that how it’s intended to work? I thought it would automatically populate the Number of Contacts field on all accounts. Or did I miss something?

Leave a Reply to DEVANSHU SOOD Cancel reply

*