Toggle Side Panel

  • Blogs
  • 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
    • Blogs
    • 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 › How to use trigger to count number of contacts associated with an account without violating the governor limits in Salesforce?

    Tagged: Account, Contacts, Governor Limits, List Account, Salesforce Trigger

    • Salesforce® Discussions

      How to use trigger to count number of contacts associated with an account without violating the governor limits in Salesforce?

      Posted by Marziya on January 14, 2020 at 2:07 PM

      How to use trigger to count number of contacts associated with an account without violating the governor limits in Salesforce?

      MOHIT replied 6 years, 9 months ago 2 Members · 1 Reply
      • Account
      • Contacts
      • Governor Limits
      • List Account
      • Salesforce Trigger
    • 1 Reply
    • MOHIT

      Member
      January 15, 2020 at 2:06 PM

      trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
      //---> above handling all states which could see a contact added to or removed from an account

      //---> on delete we use Trigger.Old, all else, Trigger.new
      List<Contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;

      //---> the Set class rocks for finding the unique values in a list
      Set<Id> acctIds = new Set<Id>();

      for (Contact c : contacts) {
      //yes, you can have a contact without an account
      if (c.AccountId != null) {
      acctIds.add(c.AccountId);
      }
      }

      List<Account> acctsToRollup = new List<Account>();

      //****** Here is the Aggregate query...don't count in loops, let the DB do it for you*****
      for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount
      FROM Contact
      WHERE AccountId in: acctIds
      GROUP BY AccountId]){
      Account a = new Account();
      a.Id = (Id) ar.get('AcctId'); //---> handy trick for updates, set the id and update
      a.Contact_Count__c = (Integer) ar.get('ContactCount');
      acctsToRollup.add(a);
      }

      //----> probably you'll want to do a little more error handling than this...but this should work.
      update acctsToRollup;

      }

    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

    Popular Salesforce Blogs

    How to Choose the Best Salesforce Consulting Company: A Complete Guide

    Blog in Salesforce integration

    Selecting the best Salesforce consulting company is essential for any company, enterprises or startups trying to get the most out of its Salesforce investment. Finding…

    best Salesforce consulting company, best Salesforce consulting partner, Custom Salesforce Development Services, Salesforce Consulting Companies in the USA, Salesforce Consulting Company
    AwsQuality Aug 1, 2025
    480  Views

    How Virtual Salesforce Administrators Help with Automation

    Blog in Salesforce Admin

    Robotic Process Automation (RPA) for Salesforce is a tool that allows you to automate certain tasks. This virtual Salesforce administrator can be helpful if you…

    Automatable Tasks, Automate Tasks, Automation, Automation Tools and Integrations, Business Applications
    Apphienz Sep 20, 2022
    1,809  Views
    Transaction Security in Salesforce

    Transaction Security in Salesforce

    Blog in Salesforce Security

    Introduction Transaction Security is a security feature that observes the activity in your Salesforce org in real-time to catch the potential security issue based on…

    API Event, API Queries, Condition Builder, Create Conditions, Customized Security
    Arun Nov 18, 2021
    5,392  Views

    Popular Salesforce Videos

    Which Objects are in the Salesforce Sales Cloud and Service Cloud?

    Which Objects are in the Salesforce Sales Cloud and Service Cloud?

    Video in Salesforce Cloud Platform

    Salesforce comes with a large number of standard objects that are integral to Salesforce, but you can also create custom objects. We’ve compiled a list…

    Salesforce Tutorial, Salesforce Sales Cloud, Salesforce Service Cloud, SOAP, Apex SOAP API
    Abhay Jan 19, 2018
    5,137  Views
    14 Questions Asked in 95% Salesforce Interviews

    14 Questions Asked in 95% Salesforce Interviews

    Video in Salesforce Stories

    In this video, Dan Croitor provides a list of 14 questions that are typically asked in Salesforce interviews. He goes over each question in detail,…

    Salesforce Training, Salesforce Tutorial, Salesforce Jobs, salesforce, Salesforce Video
    Rahul kapoor Oct 21, 2022
    3,483  Views
    What Is Salesforce Industries (Vlocity)?

    What Is Salesforce Industries (Vlocity)?

    Video in Salesforce Stories

    Vlocity (Salesforce Industries) offers pre-built solutions on the Salesforce platform that solve the needs of particular industries that require CRMs to be heavily customized beyond…

    Salesforce Training, Salesforce Tutorial, salesforce, CRM, salesforce platform
    Cyntexa Sep 5, 2025
    437  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®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.