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

    Salesforce Implementation Guide - All You Need to know

    Blog in Salesforce Implementation

    In 2021, it is safe to say that the world revolves around extracting, accessing, and managing data. It has rightly been called the “digital gold”…

    B2C Marketing Process, Boost your Revenue, Build Custom Objects, Business Management, Business Needs
    SP May 13, 2021
    2,387  Views

    Salesforce Relationships in 2023 | The Ultimate Guide

    Blog in Salesforce

    Salesforce is an effective CRM application that gives companies the ability to manage client interactions and data. Relationships are a core component of the Salesforce…

    Child Object, Client Interactions, Connect Salesforce, CRM Application, Customer Data
    Arpit Jun 1, 2023
    1,124  Views

    What is Salesforce Scheduler in 2024?

    Blog in Salesforce Tools

    In today's fast-paced business landscape, managing schedules and appointments can be a labyrinth of complexity, impacting customer experience and operational efficiency. Salesforce Scheduler is the solution for…

    Apex Scheduler, Appointment Booking, Automated Scheduling, Customer Experience, Customer-Centric Scheduling
    Routine Automation May 21, 2024
    467  Views

    Popular Salesforce Videos

    How To Create Workflow Rules In Salesforce | Salesforce Workflow Rules

    How To Create Workflow Rules In Salesforce | Salesforce Workflow Rules

    Video in Salesforce Training

    This video walks through the steps to create a Workflow Rule within the Process Automation section of Lightning Settings in Salesforce. Get answers to the…

    Salesforce Training, Salesforce Tutorial, Salesforce Lightning, Process Automation, salesforce
    Algoworks Sep 16, 2021
    1,439  Views
    TrailheaDX 2020 Opening Film | Salesforce

    TrailheaDX 2020 Opening Film | Salesforce

    Video in Trailhead

    Now more than ever, Trailblazers are using the Salesforce Platform from anywhere in the world to collaborate, innovate, make a change, and build a brighter…

    salesforce, salesforce platform, Salesforce Event, Salesforce Video, Trailblazers
    Nauman Jun 29, 2020
    1,847  Views
    What are Roll-up Summary Fields in Salesforce? | How to create them?

    What are Roll-up Summary Fields in Salesforce? | How to create them?

    Video in Others, Salesforce Training

    In this video, Shrey is not only teaching but also demonstrating to you "What are Roll-up Summary Fields in Salesforce?". You will be learning: 1.…

    Salesforce Training, salesforce, Salesforce Video, Salesforce Learning, Journey with Salesforce
    Rupal Kakkar Sep 28, 2020
    1,793  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.