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
  • 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
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › I want to count related Contact in a Account.How can i do this in salesforce Apex Code?

    Tagged: Account, Contact, Contact Record, Salesforce Apex Code

    • Salesforce® Discussions

      I want to count related Contact in a Account.How can i do this in salesforce Apex Code?

      Posted by Suraj on April 10, 2017 at 10:02 AM

      I want to count related Contact in a Account.How can i do this in Apex Code?

      Subhendu replied 8 years, 4 months ago 3 Members · 2 Replies
      • Account
      • Contact
      • Contact Record
      • Salesforce Apex Code
    • 2 Replies
    • Saurabh

      Member
      April 12, 2017 at 5:50 AM

      Hi Suraj,

      public class AccountProcessor {
      static public Integer count;
      @future public static void countContacts(Set<Id> AcctId ){
      List<Account> Acclist=[select name,AccountNumber from Account where Id in:AcctId];
      if(Acclist.size()>0){
      for(Account A:Acclist){
      count=0;
      count=[select count() from Contact where AccountId=:A.Id];
      A.Number_of_Contacts__c=count;//[select count() from Contact where AccountId=:A.Id];
      update(A);
      }
      }

      where Number_of_Contacts__c is the custom Field on Account which store the Number of Related Contact in Account.

      Hope this will Help You

    • [adinserter block='9']
    • Subhendu

      Member
      January 10, 2018 at 11:43 AM

      Hi Suraj,

      A much-optimized code would be –

      Public static Map<Id,Integer> countRelatedContact( Set<Id> accId){

      Map<Id,Integer> relatedCountMap = new Map<Id,Integer>();
      if(!accId.isEmpty){

      for(Account acc : [select Id, name,(Select Id, Name from Contacts) from Account where Id in:accId];){

      relatedCountMap.put(acc.Id,acc.Contacts.size());
      }

      return relatedCountMap;
      }

      This will return a Map of Account Id and # of related Contacts to that Account record. Hope this helps you as per your scenario.

      Thanks,
      Subhendu

    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
    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.