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

    Salesforce Files

    Objects Related To Salesforce Files And Connection Between Them

    Blog in Data, Lightning, Salesforce

    As we know most of the work has been shifted to Lightning Experience. The questions arise in the mind of users when Salesforce Classic provides…

    Attachments, Chatter Free license, Contact Manager, Contact related Files, Content
    Deepak Jul 21, 2020
    20,261  Views

    How Hiring a Salesforce Implementation Partner Could Benefit the Healthcare Sector

    Blog in Salesforce Implementation

    Digitalization of healthcare is a complex task due to the sector's unique challenges, such as handling various types of data and interdependent roles. Salesforce implementation…

    Application, Applications, Automation, Benefit, Care
    Concretio May 22, 2023
    1,612  Views

    Salesforce Marketing Cloud Genie — Building Real-time Relationships with Customers

    Blog in Salesforce Cloud Platform

    Salesforce Marketing Cloud is a digital platform that combines multiple tools and solutions. It provides everything you need including email marketing and audience development. Salesforce stores data…

    Automated Campaigns, Business, Centralized Account, Contacts and Leads, Customer Communication
    Hennery cavil Dec 15, 2022
    1,680  Views

    Popular Salesforce Videos

    Customized Ideas Management on Salesforce Experience Cloud | IdeasPro by Grazitti

    Customized Ideas Management on Salesforce Experience Cloud | IdeasPro by Grazitti

    Video in Salesforce Communities

    Learn how IdeasPro enables you to drive business growth by making the most of ideas on Salesforce communities, in this video.

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Community, Salesforce Learning
    Sukhwinder Singh Jul 29, 2022
    3,398  Views
    Winter 21 - Restrict Access to @AuraEnabled Apex Methods for Authenticated Users

    Winter 21 - Restrict Access to @AuraEnabled Apex Methods for Authenticated Users

    Video in Salesforce Apex, Salesforce Training

    Winter 21 - Restrict Access to @AuraEnabled Apex Methods for Authenticated Users The video explained the following Items: Action items to enable or disable the…

    Salesforce Training, Salesforce Tutorial, Salesforce Lightning, Salesforce Video, Salesforce Learning
    Jogender Nov 5, 2020
    1,430  Views
    Creation Of Validation Rules And Verifying Them In Salesforce

    Creation Of Validation Rules And Verifying Them In Salesforce

    Video in Salesforce Training

    Learn how to create and verify validation rules in Salesforce. This video will cover the following points - 1. Introduction Get a brief introduction about…

    Salesforce Training, Salesforce Tutorial, Salesforce Development, salesforce, Salesforce Development Tutorial
    Algoworks Dec 29, 2020
    8,260  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.