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

    application solution

    Popular Salesforce Blogs

    salesforce resource

    Fight Corona With These Free COVID-19 Resources | Channel your Energy Positively

    Blog in Salesforce Stories

    These are unsettling and challenging times for all of us as we see ourselves battling an invisible force. Whenever there is a discussion on apocalypse,…

    Apocalypse, Business Travels, Collaboration Tool, Conga, Corona Virus
    DemandBlue May 7, 2020
    2,802  Views

    How Salesforce CRM Can Enhance a Business?

    Blog in Salesforce

    Salesforce is among the most trendiest and trustworthy CRM solutions in the present era. We may have had heard about how young entrepreneurs make the…

    Better Customer Service, Build More Strong Relationships, Business, Business Growth, Business Process
    360 Degree May 6, 2021
    2,462  Views
    salesforce ai

    How Does AI In Salesforce Enable Users and Businesses to Meet Their Goals?

    Blog in Salesforce Einstein

    People who work in businesses have goals. The same goes for people who use the software. Organizations try to help their businesses meet their goals…

    Artificial Intelligence, Artificial Intelligence in Salesforce, Automating Process, Business Operations, Business Owners
    Melonleaf May 4, 2023
    2,019  Views

    Popular Salesforce Videos

    Run Report With Filters from UiPath Studio | Salesforce

    Run Report With Filters from UiPath Studio | Salesforce

    Video in Salesforce Integration, Salesforce Training

    New features on Salesforce Activities Extention Package from UiPath MarketPlace: https://connect.uipath.com/marketplace/components/salesforce-activities-extention-package (0:40) Use complex object in the Query and Timeout part of the Setting to…

    Salesforce Training, salesforce, Salesforce Video, big data, Salesforce Learning
    Cristian Dec 10, 2020
    2,221  Views
    Spring'21 Prior Value in Salesforce Record Triggered Flows - ISCHANGED IN Salesforce Flows

    Spring'21 Prior Value in Salesforce Record Triggered Flows - ISCHANGED IN Salesforce Flows

    Video in Salesforce Admin

    In this video, I'm talking about the Prior value feature in the Spring'21 pre-release that is for the record triggered flow.  Pre-Release Org URL: https://www.salesforce.com/form/signup/prerelease-spring21/…

    Salesforce Training, salesforce, Salesforce VIdeos, salesforce record, Salesforce Learning
    Shubham Jan 14, 2021
    2,602  Views
    Basic Concepts of Salesforce | Salesforce Training Online

    Basic Concepts of Salesforce | Salesforce Training Online

    Video in Salesforce Training

    This video session on Salesforce online training covers the basic concepts of Salesforce and will give intense knowledge on Salesforce at a very high level.…

    Salesforce Online Training, Cloud Computing, SOSL, Salesforce Professional, Salesforce Training Online
    Abhishek Sep 24, 2020
    2,153  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.