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

    salesforce Artificial Intelligence

    How Do Artificial Intelligence Systems Compare Salesforce Records?

    Blog in Others, Salesforce

    When you compare two Salesforce records, side-by-side, you can easily determine whether or not they are duplicates. However, even if you have a small number…

    Artificial Intelligence Systems, Automate, Blocking Methodology, Built-in Deduplication in Salesforce, Companies
    ildudkin Apr 29, 2021
    3,800  Views
    Salesforce Marketing Cloud

    Learn All About Salesforce Marketing Cloud and its Editions

    Blog in Salesforce Cloud Platform

    The goal of marketing is to generate interest in the goods or services that your business offers. Marketing drives sales, resulting in an influx of…

    Automated Software, Bbusiness Offers, Business Nodes, Business Offers, Business Process
    Genetrix Sep 12, 2022
    2,425  Views
    Salesforce Commerce Cloud

    Why Does an Online Retail Business Need Salesforce Commerce Cloud?

    Blog in Salesforce Cloud Platform

    The e-commerce industry is revolutionizing the way buyers shop. With time more and more people are inclined to brands that sell online or have a…

    Artificial Intelligence, B2B, B2C, Building Strategies, Business
    Melonleaf Jan 17, 2022
    3,902  Views

    Popular Salesforce Videos

    DreamTX Welcome Day 3: Customer Success | Dreamforce 2020 | Salesforce

    DreamTX Welcome Day 3: Customer Success | Dreamforce 2020 | Salesforce

    Video in Salesforce Stories, Events

    If you missed DreamTX DAY 2, then live this amazing experience here, we have covered it all for you. Digital transformation requires more than just…

    dreamforce, salesforce, Salesforce Event, Customer Success, Salesforce Video
    Pooja Dec 17, 2020
    2,383  Views
    Salesforce Marketing Cloud Connect

    Salesforce Marketing Cloud Connect

    Video in Salesforce Cloud Platform

    We explore the key benefits of Marketing Cloud Connect as the start of a four-part series surveying the power, flexibility, and personalization that can be…

    Salesforce Sales Cloud, Salesforce Service Cloud, salesforce, Salesforce Marketing Cloud, Salesforce Journey Builder
    DB Oct 9, 2020
    2,802  Views
    What is APEX? | Features of APEX | Salesforce Development Course

    What is APEX? | Features of APEX | Salesforce Development Course

    Video in Salesforce Apex, Salesforce Training

    This is the first Chapter of the Salesforce Development Course video series. In this video, Shrey has answered the following questions: What is APEX? What…

    Salesforce Training, Salesforce Tutorial, Salesforce Development, salesforce, Apex
    Prafull Jun 18, 2021
    2,083  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.