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

    application solution

    Popular Salesforce Blogs

    Salesforce Meetings

    Best Practices For An Effective Sales Presentation With Salesforce Meetings

    Blog in Salesforce

    In today’s 21st-century business domain, having a competitive and top-notch sales pitch can play a critical role in sealing your deals. No matter the nature…

    Best Practices, Business Aspect, Business Domain, Business Journey, Business World
    Alejandro Carrera Jul 14, 2021
    10,649  Views
    Sales Funnel

    How Can the Real Estate Sector Use Salesforce to Standardize Its Sales Funnel?

    Blog in Salesforce

    The pandemic has changed how businesses conduct their daily operations. Before the pandemic, the real estate industry was going strong, but its activities slowed down…

    Attracting New Leads, Automatic Backup of Records, Better Retention Strategies, Bsiness Departments, Business
    Myrah May 13, 2021
    4,221  Views

    Salesforce Marketing Cloud for Healthcare's Personalized Revolution

    Blog in Others

    In today's competitive landscape, engaging patients and driving outcomes is vital. But legacy systems and siloed data often stand as formidable barriers. Enter Salesforce Marketing…

    Appointment Reminders, Collaboration, Customer Journeys, Data Integration, Data Privacy
    Emorphis Feb 29, 2024
    1,329  Views

    Popular Salesforce Videos

    How to Create an Object in Salesforce Lightning Experience?

    How to Create an Object in Salesforce Lightning Experience?

    Video in Lightning, Salesforce Training

    Salesforce has always emphasized on declarative development since the beginning. The motto is that enterprise software should be easy to use for customers. With “clicks…

    salesforce training videos for beginners, Salesforce Training Videos, Free Salesforce Training, Salesforce Lightning Experience, Salesforce Objects
    Shiv Kumar Sharma May 11, 2018
    5,780  Views
    Export Data from Salesforce using Data Loader - Step by Step Process

    Export Data from Salesforce using Data Loader - Step by Step Process

    Video in Data, Salesforce Training

    In this video learn how to Export Data from Salesforce to CSV using Data Loader. You can install Data Loader on Windows or Mac. And…

    Salesforce Data Management, Salesforce Data Migration, Salesforce Data Loader, Data Management In Salesforce, Salesforce Data Types
    Jogender Feb 9, 2018
    2,294  Views
    Searching for Records In Salesforce

    Searching for Records In Salesforce

    Video in Salesforce Training

    You can search globally across all of Salesforce for matching records to your search term. Or you can search within a specific object. You will…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Video, Salesforce Learning
    Abhishek Jan 27, 2022
    1,667  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

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