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

    A Guide to Integrate Salesforce Service Cloud with Jira

    Blog in Salesforce integration

    Joel is the head of customer support at a leading cybersecurity software provider company and he manages a team of over 400 service reps. His…

    jira, Salesforce Integration, Salesforce Integration with Jira, Salesforce Jira Integration, Service Cloud
    Shubham Sep 12, 2019
    3,655  Views
    Test Classes in Salesforce

    Test Classes in Salesforce - Learn All About it Here

    Blog in Salesforce Apex

    Content   What is a Test class in Salesforce?  What is the need of a Test class in Salesforce?  How to write Test class in Salesforce? …

    Apex Test Class, Assert Statement, Batch Class, Best Practices, Client Requirements
    ABHISHEK SHUKLA Jul 5, 2022
    2,880  Views
    Become a Salesforce expert

    Becoming a Salesforce Expert - Part 1

    Blog in Salesforce Implementation

    It is safe to say that you are pondering over whether a future in Salesforce is the correct move for you or not. Perhaps you…

    SALES, Salesforce, Salesforce Customization, Salesforce Development, Salesforce Guide
    Vivek Feb 27, 2017
    6,336  Views

    Popular Salesforce Videos

    Tips to Crack the Salesforce Administrator Certification Exam in One Go

    Tips to Crack the Salesforce Administrator Certification Exam in One Go

    Video in Salesforce Certifications

    Do you want to become Salesforce administrator certified? So in this video Shrey has shared the important topics to crack the Salesforce Administrator Exam. Watch…

    Salesforce Training, Salesforce Tutorial, Salesforce Certification, Salesforce Administrator, Salesforce Video
    Jogender Sep 2, 2021
    1,828  Views
    Demystifying OAuth and Connected Apps

    Demystifying OAuth and Connected Apps

    Video in Salesforce Development

    Often times, the first thing an application needs to do is authenticate its users. Developers building integrations with Salesforce need a simple and secure way…

    Salesforce Integration, How To Learn Salesforce Development, Salesforce Learning Steps For Beginners, Easy Way To Learn Salesforce, Best Salesforce Tutorials
    Hazel Feb 4, 2019
    2,385  Views
    Integrating System with Salesforce Commerce Cloud

    Integrating System with Salesforce Commerce Cloud

    Video in Salesforce Integration, Salesforce Cloud Platform

    Designed for retail as well as eCommerce, Salesforce Commerce Cloud has become the best solution for delivering the ultimate shopping experience to the customers. By…

    Salesforce Training, Salesforce Tutorial, Salesforce Integration, salesforce, Salesforce Cloud
    Cyntexa Dec 2, 2021
    2,350  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.