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

    Salesforce Features To Help Your Nonprofit Organization Succeed

    Blog in Salesforce, Salesforce Cloud Platform

    The impacts of non-profit organizations are undeniable, especially when it comes to the people and communities they serve. They have likely changed and touched the…

    Build Better Applications, Business Organizations, Collaboration, Communication and Cloud, communities
    Apphienz Mar 30, 2021
    1,933  Views

    Salesforce Apex Trigger

    Blog in Salesforce Apex

    By using Apex triggers, you may execute custom code before and after specific Salesforce record events, such as inserts, changes, and deletes. Apex offers trigger…

    APEX Programming Language, Apex Script, Apex Trigger, Apex Trigger Code, Child Objects
    Hexaview Dec 23, 2022
    3,019  Views
    Best Practices for Integrating AI into Salesforce CRM

    Integrating AI into Salesforce CRM: Best Practices

    Blog in AI and ML

    Table of Contents Introduction Best practices for integrating AI into Salesforce CRM Understand Your Business Objectives Choose the Right AI Tools Prepare Your Data Start…

    Automation Insights, CCPA Compliance, Continuous Improvement, Customer Interactions, Customer Trust
    Kizzy Consulting Jul 24, 2024
    650  Views

    Popular Salesforce Videos

    Security in Salesforce - What Developers Must Know

    Security in Salesforce - What Developers Must Know

    Video in Salesforce Security

    Security in Salesforce 0:00​ Introduction 3:00​ Agenda 4:05​ Data security 5:18​ User Mode & System Mode 9:13​ CRUD & FLS Data Model 10:45​ Enforcing CRUD…

    Salesforce Training, Apex Class, Salesforce Lightning, Salesforce Developers, Salesforce Video
    Prafull May 4, 2021
    1,893  Views
    Salesforce India Engineer Salary Revealed | Salary Break Up | Company Review

    Salesforce India Engineer Salary Revealed | Salary Break Up | Company Review

    Video in Salesforce Stories

    In this video, Soumyajit Bhattacharyay has given a detailed review of Salesforce. Talked about the tech stack that they work in. Talked about their offices…

    salesforce, Salesforce Developers, Salesforce Video, Salesforce Learning, Salesforce Job
    Nauman Apr 30, 2021
    2,546  Views
    Salesforce Marketing Cloud Email Studio | Marketing Cloud Tutorial

    Salesforce Marketing Cloud Email Studio | Marketing Cloud Tutorial

    Video in Marketing, Salesforce Cloud Platform

    Learn all about Salesforce marketing cloud email studio. In this video, the following points will be covered - 1. Overview Of Email Studio & Content…

    Salesforce Training, Salesforce Tutorial, Salesforce Marketing Cloud, Salesforce Video, Salesforce Learning
    Algoworks Oct 14, 2021
    3,119  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.