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

    New Summer '22 Salesforce Flow Updates

    Blog in Salesforce Releases

    Numerous areas of Salesforce that formerly needed specialized programmers no longer necessitate the hiring of developers. Salesforce Flow Builder is now the key tool for…

    Apex-Defined Invocable Actions, Applications, Automation, Create Screen Flows, Custom Component
    Apphienz Jun 13, 2022
    2,512  Views
    What is Salesforce?

    What is Salesforce?

    Blog in Salesforce Products, Salesforce Tools

    What is Salesforce? Salesforce is a cloud-based Customer Relationship Management (CRM) software that makes solutions available for sales, service, marketing, collaboration, analytics, and building custom…

    Cloud Computing Salesforce, Force.com Platform Apex Limits, Miscellaneous Apex Limits, Per-Transaction Apex Limits, Per-Transaction Certified Managed Package Limits
    Archit Feb 16, 2018
    16,211  Views
    Sender Authentication Package

    A Guide to Sender Authentication Package (SAP) for Salesforce Marketing Cloud

    Blog in Salesforce Cloud Platform

    Salesforce has released a sender authentication package for Marketing Cloud that is designed to help ensure the authenticity of email messages sent from Salesforce. This…

    Auto-response, Business Email Marketing, Business Needs, Cloud Pages, Custom Domain
    SP Oct 25, 2022
    1,751  Views

    Popular Salesforce Videos

    How to Send a Slack Message from Salesforce Marketing Cloud

    How to Send a Slack Message from Salesforce Marketing Cloud

    Video in Salesforce Training

    In this video, Cameron Robert shows how to use the Slack Message API to send a message from Salesforce Marketing Cloud to a Slack Channel.…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Marketing Cloud, Salesforce Video
    Cameron Jul 25, 2022
    2,471  Views
    How To Assign Leads in Salesforce | Overview of 3 Main Lead Routing Solutions

    How To Assign Leads in Salesforce | Overview of 3 Main Lead Routing Solutions

    Video in Salesforce Development

    Salesforce offers several options on how to automatically assign incoming leads to a suitable sales manager. But what to choose: out-of-the-box features, external AppExchange app,…

    Lead Assignment rule in Salesforce, Salesforce Lead Assignment Rule, Salesforce Custom Development, Lead Assignment Rule, Lead Routing
    ENWAY Jul 3, 2024
    1,190  Views
    How Cyntexa Helped Diet Meal And Food Ordering Service Providers Offer Their Users The Ability To Order Online | Salesforce Commerce Cloud

    How Cyntexa Helped Diet Meal And Food Ordering Service Providers Offer Their Users The Ability To Order Online | Salesforce Commerce Cloud

    Video in Salesforce Stories

    It is important to keep your business at the top with the latest technical advancements. Presenting you a case study showcasing how Cyntexa Helped Diet…

    Salesforce Training, salesforce, salesforce services, Salesforce Tutorials, Salesforce Video
    Cyntexa Jan 16, 2023
    1,245  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.