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 can I create rollup Summary field in Account Object with Opportunity Amount using Triggers?

    Tagged: Account, Account Object, Rollup, Rollup Summary, Salesforce Objects, Salesforce Opportunity, Salesforce Trigger

    • Salesforce® Discussions

      How can I create rollup Summary field in Account Object with Opportunity Amount using Triggers?

      Posted by Aman on July 13, 2017 at 1:21 PM

      I want to create a rollup summary field using triggers?

      Aileen Kim replied 2 years, 7 months ago 4 Members · 3 Replies
      • Account
      • Account Object
      • Rollup
      • Rollup Summary
      • Salesforce Objects
      • Salesforce Opportunity
      • Salesforce Trigger
    • 3 Replies
    • Shaharyar

      Member
      August 25, 2017 at 9:37 AM

       

      Hi Aman,

      First make a field on account object with name "OppAmount" and then use this code:

      #Trigger
      trigger calAmount on Opportunity (after insert, after update, after delete) {
      Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
      Set<Id> acctIds = new Set<Id>();
      List<Opportunity> opptyList = new List<Opportunity>();
      if(trigger.isUpdate || trigger.isInsert){
      for(Opportunity oppty : trigger.New){
      if(oppty.AccountId != null){
      acctIds.add(oppty.AccountId);
      }
      }
      }
      if(trigger.isDelete){
      for(Opportunity oppty : trigger.old){
      if(oppty.AccountId != null){
      acctIds.add(oppty.AccountId);
      }
      }
      }
      if(acctIds.size() > 0){
      opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
      for(Opportunity oppty : opptyList){
      if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
      acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
      }
      acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
      }
      List<Account> acctList = new List<Account>();
      acctList = [SELECT OppAmount__c FROM Account WHERE Id IN: acctIds];
      for(Account acct : acctList){
      List<Opportunity> tempOpptyList = new List<Opportunity>();
      tempOpptyList = acctIdOpptyListMap.get(acct.Id);
      Double OppAmount = 0;
      for(Opportunity oppty : tempOpptyList){
      if(oppty.Amount != null){
      OppAmount += oppty.Amount;
      }
      }
      acct.OppAmount__c = OppAmount;
      }
      update acctList;
      }
      }

       

      Hope this may help u.

    • Parul

      Member
      September 6, 2018 at 12:36 PM

      Hello Aman,

      First make a field on account object with name “OppAmount” and then use this code:

      I have written a sample trigger for you:

      #Trigger
      trigger totalAmount on Opportunity (after insert, after update, after delete) {
      Map<Id, List<Opportunity>> acctIdOppoListMap = new Map<Id, List<Opportunity>>();
      Set<Id> acctIds = new Set<Id>();
      List<Opportunity> oppoList = new List<Opportunity>();
      if(trigger.isUpdate || trigger.isInsert){
      for(Opportunity oppo : trigger.New){
      if(oppo.AccountId != null){
      acctIds.add(oppo.AccountId);
      }
      }
      }
      if(trigger.isDelete){
      for(Opportunity oppo : trigger.old){
      if(oppo.AccountId != null){
      acctIds.add(oppo.AccountId);
      }
      }
      }
      if(acctIds.size() > 0){
      oppoList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
      for(Opportunity oppo : oppoList){
      if(!acctIdOppoListMap.containsKey(oppo.AccountId)){
      acctIdOppoListMap.put(oppo.AccountId, new List<Opportunity>());
      }
      acctIdOppoListMap.get(oppo.AccountId).add(oppo);
      }
      List<Account> acctList = new List<Account>();
      acctList = [SELECT Amount__c FROM Account WHERE Id IN: acctIds];
      for(Account acct : acctList){
      List<Opportunity> tempOppoList = new List<Opportunity>();
      tempOppoList = acctIdOppoListMap.get(acct.Id);
      Double OpportunityAmount = 0;
      for(Opportunity oppo : tempOppoList){
      if(oppo.Amount != null){
      OpportunityAmount += oppo.Amount;
      }
      }
      acct.Amount__c = OpportunityAmount;
      }
      update acctList;
      }
      }

      Hope, this will solve your problem.

      Thanks!!

    • Aileen Kim

      Member
      June 22, 2023 at 1:01 PM

      Hi @shaharyar and @parul-2 the code is very helpful however, it also calculates Closed Lost Opportunities. How do I filter it and only calculate the Opportunities that are in Closed Won and Invoice Sent? Thank you.

    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

    Data Modeling in Salesforce

    Learn All About Data Modeling in Salesforce

    Blog in Data, Salesforce Admin

    Introduction A data model is a way to store/represent data in a database consisting of tables with columns and rows. In Salesforce, data is stored…

    Business Requirements, Child Object, Contact, Create a Custom Object, Creating Records
    Arun Jan 28, 2022
    3,886  Views

    Why all Salesforce Users Should Integrate Their Systems

    Blog in Others

    A common scenario with organizations using Salesforce is having a scenario where employees from different departments, are not able to access the same accurate, real-time…

    Cloud Platform, CRM System, Lightning Component, Real-Time Salesforce Environment, Salesforce Integration
    Cloud Data May 23, 2019
    2,733  Views

    Salesforce Agentforce: Revolutionizing Customer Service

    Blog in Salesforce Products

      In today’s fast-paced digital world, companies are constantly searching for efficient ways to streamline operations, enhance customer service, and drive productivity. For organizations heavily…

    Customer Service, Salesforce Agentforce, Salesforce Partner USA, salesforce partners
    Kizzy Consulting Aug 1, 2025
    154  Views

    Popular Salesforce Videos

    How To Become A Salesforce Administrator | Salesforce Administrator Training

    How To Become A Salesforce Administrator | Salesforce Administrator Training

    Video in Salesforce Admin

    This "How to become a Salesforce Administrator" video will help you understand who is a Salesforce Administrator and what are their roles and responsibilities. It…

    Salesforce Tutorial, Salesforce Administrator Training, Salesforce Admin, Salesforce Video, Salesforce Learning
    Rupal Kakkar May 21, 2021
    1,505  Views
    Is Trailhead enough to crack Salesforce Interview?

    Is Trailhead enough to crack Salesforce Interview?

    Video in Salesforce Certifications

    In this video, Shrey answered one of the most asked questions related to Salesforce Interview. Let's see how beautifully Shrey answered this Question. We hope…

    Salesforce Training, Salesforce Jobs, Salesforce Certification, salesforce, Trailhead
    Jogender Apr 14, 2021
    1,479  Views
    Dreamforce 2019 - An Overview!

    Dreamforce 2019 - An Overview!

    Video in Others

    Salesforce Dreamforce2019 is scheduled for November 19-22 in San Francisco, California at the Moscone Center. #Dreamforce is one of the largest cloud and SaaS-centric conferences…

    Dreamforce Event, Salesforce Events, Dreamforce 2019, Salesforce Enthusiast, DF 19
    Algoworks Nov 14, 2019
    2,273  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.