• Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce Consultants
      • Marketing Cloud Consultants
      • Service Cloud Consultants
      • Experience Cloud Consultants
      • Analytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud Consultants
      • Financial Service Cloud Consultants
      • Health Cloud Consultants
      • Commerce Cloud Consultants
      • Manufacturing Cloud Consultants
  • Jobs
  • Marketplace
  • Stories
  • Top Bloggers 2022
  • Advertise With Us
  • Quizzes
  • Discussions
  • Groups
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce Consultants
        • Marketing Cloud Consultants
        • Service Cloud Consultants
        • Experience Cloud Consultants
        • Analytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud Consultants
        • Financial Service Cloud Consultants
        • Health Cloud Consultants
        • Commerce Cloud Consultants
        • Manufacturing Cloud Consultants
    • Jobs
    • Marketplace
    • Stories
    • Top Bloggers 2022
    • Advertise With Us
    • Quizzes
    • Discussions
    • Groups

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

    Reply

    Subscribe

    airslate

    SALESFORCE PRODUCT EXPERTISE

    • Top Salesforce Consultants
    • Top Salesforce Experience Cloud Consultants
    • Top Salesforce Analytics Cloud Consultants
    • Top Salesforce Service Cloud Consultants
    • Top Salesforce Marketing Cloud Consultants

    SALESFORCE INDUSTRY EXPERTISE

    • Top Salesforce Consultants
    • Top Salesforce Manufacturing Cloud Consultants
    • Top Salesforce Non-Profit Cloud Consultants
    • Top Salesforce Health Cloud Consultants
    • Top Salesforce Financial Service Cloud Consultants
    • Top Salesforce Commerce Cloud Consultants

    Popular Salesforce Blogs

    How Does Salesforce Genie Impact Your Business?

    Blog in Data, Data Migration, Salesforce

    Salesforce Genie, a real-time platform, powers customer magic. Your teams are able to meet your customers where they are unlike ever before by harmonizing data…

    Best Practices, Business, Commerce, Compliance Standards, Customer 360
    Apphienz Nov 15, 2022
    1,264  Views
    Salesforce Flow

    Related Records Updation with the Help of Salesforce Flow

    Blog in Salesforce Apex

    Hello all, In this blog, we will get to know how to update records using Salesforce flow. For updating the related object records we can…

    Account Lookup Field, Account Records, Accounting Field, Contact Lookup Field, Contact Record
    bhawana sharma Dec 5, 2022
    1,027  Views
    Marketing Cloud Content

    Salesforce Marketing Cloud Content Builder Can Help You Succeed

    Blog in Marketing, Salesforce Cloud Platform

    Salesforce Marketing Cloud is a robust marketing automation platform that enables firms to drive their marketing activities and convert leads into sales. The Content Builder,…

    AI, AMPScript, API, Applications, Approvals
    Cymetrix Jun 10, 2021
    2,800  Views

    Popular Salesforce Videos

    Top 4 Ways Sales Leaders at Salesforce Use Einstein Analytics

    Top 4 Ways Sales Leaders at Salesforce Use Einstein Analytics

    Video in Salesforce Einstein

    Here's a peek behind the curtain at four of the top ways sales leaders at Salesforce use Einstein Analytics to manage pipeline and coach their…

    Salesforce Training, salesforce, Salesforce Einstein, Salesforce Video, sales
    Neha Yadav Sep 29, 2020
    710  Views
    Salesforce Tutorial - Data Loader Installation On Windows - with Zulu Open JDK

    Salesforce Tutorial - Data Loader Installation On Windows - with Zulu Open JDK

    Video in Salesforce Training

    Data Loader is utility to perform bulk database operation. Recently Salesforce has changed the way data loader need to be install. I explain end to…

    Introduction To Salesforce
    Sam J Apr 30, 2020
    5,407  Views
    Salesforce Apex Class | Salesforce Development | SFDC Development

    Salesforce Apex Class | Salesforce Development | SFDC Development

    Video in Salesforce Apex

    This video covers how to create an Apex Class in Salesforce and Execute the Apex Class. Do let us know in the comment section if…

    Salesforce Training, Salesforce Tutorial, Salesforce Development, Salesforce Apex, salesforce
    Abhishek Dec 20, 2021
    1,765  Views
    Footer Forcetalks logo

    [email protected]

    Forcetalks Android App
    Forcetalks IOS App

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2022

    Top Salesforce® Bloggers 2021

    Tips to choose Best Salesforce® Consulting Company

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    Submissions

    Write a Blog

    Start a New Discussion

    Post a Video

    Post an Infographic

    © 2023 - 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®.

    Forum Description

    Report

    There was a problem reporting this post.

    Harassment or bullying behavior
    Harassment or bullying behavior
    Contains mature or sensitive content
    Contains misleading or false information
    Mobile App Reported Contents
    Contains abusive or derogatory content
    Contains spam, fake content or potential malware

    Block Member?

    Please confirm you want to block this member.

    You will no longer be able to:

    • See blocked member's posts
    • Mention this member in posts
    • Invite this member to groups
    • Message this member
    • Add this member as a connection

    Please note: This action will also remove this member from your connections and send a report to the site admin. Please allow a few minutes for this process to complete.

    Report

    You have already reported this .
    Clear Clear All
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.