Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How can we count number of contacts associated with accounts?

    Tagged: Account Detail, Contact, Contact Record, Count, Person Accounts, Salesforce Customization, Salesforce Development

    • Salesforce® Discussions

      How can we count number of contacts associated with accounts?

      Posted by saloni gupta on July 13, 2017 at 12:19 PM

      Suppose we have a list of account and we want to count the number of contacts associated with each account?

      Rahul replied 7 years, 10 months ago 5 Members · 5 Replies
      • Account Detail
      • Contact
      • Contact Record
      • Count
      • Person Accounts
      • Salesforce Customization
      • Salesforce Development
    • 5 Replies
    • Aman

      Member
      July 13, 2017 at 12:20 PM

      Hello Saloni,

      List<Account> acc=[select Name , (Select LastName from contacts) from Account Limit 10];
      for(Account acct:acc){
      system.debug('####'+acct.contacts.size());
      }

       

    • Raghav Chaturvedi

      Member
      July 13, 2017 at 1:10 PM

      trigger london1 on Contact (after insert,after delete) {

      list li=new list();
      if(trigger.isInsert)
      {
      for(contact c:trigger.new)
      {
      li.add(c.AccountId);
      }

      list con=[select name,(select id from contacts) from account where id in:li];
      for(account acc:con)
      {
      list c=acc.contacts;
      system.debug('account:'+acc.name+'Count_of_contact'+c.size());
      acc.count_of_contacts__c=c.size();
      update acc;
      }
      }
      }

      this will help you

    • shariq

      Member
      July 13, 2017 at 1:43 PM

      Hi Saloni,

      The code for your answer is : -

      public class RelatedList
      {
      public static void ListContact()
      {
      List accList = [SELECT Name, (SELECT LastName FROM Contacts ) FROM Account];
      for(Account acc : accList)
      {
      System.debug('+++'+ acc.contacts.size());
      }
      }
      }

      • This reply was modified 8 years, 6 months ago by  shariq.
    • shariq

      Member
      July 14, 2017 at 9:34 AM

      Hi Saloni,

      There is one more way to count number of contacts in account using SOQL query :-

      List contact = [Select Count(Id),Account.Name from Contact where Account.Id != null GROUP BY Account.Name];

      • This reply was modified 8 years, 6 months ago by  shariq.
    • Rahul

      Member
      March 16, 2018 at 11:17 AM

      Hi,

      simple query :-
      SELECT count(Id) FROM Contact Where AccountID='00141000015cS58'
      ---Soql query to count total contact related to an account -----

      List<AggregateResult> results=[SELECT count(Id) FROM Contact Where AccountID='00141000015cS58']; //count() return AggregateResult type value
      integer total_contact = Integer.valueOf(results[0].get('expr0') + '');
      //integer total_contact=(Integer) results[0].get('expr0');
      system.debug('total contact of Internal Queries : '+total_contact);

      And trigger  to show count on a custom field:

      trigger FindContactsCount on Contact (after insert, after update, after delete) {

      List<Contact> modContacts = Trigger.isDelete ? Trigger.old:Trigger.new;
      Set<Id> accIds = new Set<Id>();
      for (Contact c: modContacts) {
      accIds.add(c.AccountId);
      }
      List<Account> modAccounts = [Select Name, (Select Id from Contacts) from Account WHERE Id in: accIds ];

      for (Account a: modAccounts) {
      List<Contact> s = a.Contacts;
      System.debug('Account:' + a.Name + ' No. of Contacts: '+ s.size());
      a.No_of_Contacts__c = s.size();
      }
      }

      Hope this code will help you and others 🙂

       

       

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    Inbound web services

    Inbound and Outbound Web Services in Salesforce

    Blog in Others, Salesforce

    Introduction to Inbound web Service It's associate degree inbound call to Salesforce, however outward-bound call to the external system. Here, the external system is the…

    AWS, Chatter Extension, HyperText Transfer Protocol, Inbound Web Services, Internet Services
    Ratnesh Jul 10, 2020
    17,194  Views
    How to Delete Components from a Managed - Released Package in Salesforce?

    How to Delete Components from a Managed - Released Package?

    Blog in Salesforce customization, Salesforce Implementation

    Deletion of components is required when a component is no longer in use or a particular component has conflicted with some other components with the…

    Custom Objects, Custom Setting, Custom Settings in Salesforce, Custom Tabs, Lightning Components
    Aman May 11, 2018
    18,944  Views

    How Salesforce Integration with Marketing Automation Tools Can Boost Your Business?

    Blog in Salesforce integration

    In the fast-paced digital age, businesses are constantly seeking ways to enhance their marketing efforts and drive growth. Integrating Salesforce with marketing automation tools is…

    automation tools, Campaign Analytics, Conversion Rates, Customer Engagement Tools, Customer Insights
    Aleksandar Jul 11, 2024
    679  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.