APEX Aggregate Query to get Sum of Amount of all Opportunities of Account when record is updated Or Inserted Using Apex Trigger in Salesforce | Write Apex Trigger on Opportunities to Sum of Amount of all Opportunities of related Account Using Aggregate Query in Salesforce

3,038 views


Hey guys, today in this post we are going to learn about How to Sum of Amount of all Opportunities of related Account Using Aggregate Query in Salesforce.

Aggregate functions in SOQL, such as SUM() and MAX(), allow you to roll up and summarize your data in a query. For more information on aggregate functions, see Aggregate Functions in the Salesforce SOQL and SOSL Reference Guide.

You can use aggregate functions without using a GROUP BY clause. For example, you could use the AVG() aggregate function to find the average Amount for all your opportunities. To know more details about Aggregate Query, Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

Final Output →

Aggregate Query to get Sum of Amount -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

 

Create Apex Trigger →

Step 1:- Create Apex Controller : opportunityTrigger.apxt

SFDX:Create Apex Class >> New >> opportunityTrigger.apxt

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

opportunityTrigger.apxt [Apex Class]

  1.    TRIGGER opportunityTrigger ON Opportunity (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
  2.     IF(TRIGGER.isBefore){
  3.         system.debug('I am inside before');
  4.     }ELSE IF(TRIGGER.isAfter){
  5.         system.debug('I am inside after');
  6.         IF(TRIGGER.isUpdate){
  7.             opportunityHandlerCtrl.updateOpportunity(TRIGGER.new);
  8.         }ELSE IF(TRIGGER.isInsert){
  9.            opportunityHandlerCtrl.updateOpportunity(TRIGGER.new); 
  10.         }
  11.     }
  12. }

 

 

Create Apex Class Controller →

Step 2:- Create Apex Controller : opportunityHandlerCtrl.apxc

SFDX:Create Apex Class >> New >> opportunityHandlerCtrl.apxc

opportunityHandlerCtrl.apxc [Apex Class]

  1.     public class opportunityHandlerCtrl {
  2.  
  3.     public static void updateOpportunity(List<Opportunity> oppList){
  4.  
  5.         Set<Id> setId = NEW Set<Id>(); 
  6.         Map<Id, Decimal> accmap = NEW Map<Id, Decimal>();
  7.         DECIMAL mydecval = 0;
  8.  
  9.         FOR(Opportunity oppObj : oppList){
  10.             setId.add(oppObj.AccountId);
  11.         }
  12.         system.debug('setId ' + setId);
  13.         List<AggregateResult> agrObj =[SELECT AccountId, SUM(Booking_Amount__c) totalAmount FROM Opportunity WHERE AccountId=:setId GROUP BY AccountId];
  14.         FOR(Opportunity opt:oppList){
  15.  
  16.             FOR(AggregateResult agr:agrObj){
  17.                 mydecval = (DECIMAL)agr.get('totalAmount');
  18.                 accmap.put((Id)agr.get('AccountId') ,(DECIMAL)agr.get('totalAmount'));
  19.                 Account accItem = NEW Account(); 
  20.                 accItem.Id=(Id)agr.get('AccountId'); 
  21.                 accItem.Total_Booking_Revenue__c = mydecval;
  22.  
  23.                 //system.debug('accmap44 ' + accItem);
  24.                 //system.debug('keySet111 ' + accmap.keySet());
  25.                 // system.debug('keyVal ' + accmap.values());
  26.                 // 
  27.                 Campaign cpm = NEW Campaign();
  28.                 cpm.Id = opt.CampaignId;
  29.                 cpm.ActualCost=mydecval;
  30.                 system.debug('cpm22 ' + cpm);
  31.                 UPDATE cpm;                
  32.                 UPDATE accItem;
  33.             }
  34.  
  35.         }  
  36.  
  37.         system.debug('mydecval!! ' + mydecval);
  38.  
  39.     }
  40.  
  41. }

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do you use sum aggregate function?

You can sum a column of numbers in a query by using a type of function called an aggregate function. Aggregate functions perform a calculation on a column of data and return a single value.

What is aggregate result?

An aggregate amount or score is made up of several smaller amounts or scores added together.

What are aggregate queries?

An aggregate query is a method of deriving group and subgroup data by analysis of a set of individual data entries. The term is frequently used by database developers and database administrators.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment