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.
Final Output β
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
opportunityTrigger.apxt [Apex Class]
TRIGGER opportunityTrigger ON Opportunity (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
IF(TRIGGER.isBefore){
system.debug('I am inside before');
}ELSE IF(TRIGGER.isAfter){
system.debug('I am inside after');
IF(TRIGGER.isUpdate){
opportunityHandlerCtrl.updateOpportunity(TRIGGER.new);
}ELSE IF(TRIGGER.isInsert){
opportunityHandlerCtrl.updateOpportunity(TRIGGER.new);
}
}
}
Create Apex Class Controller β
Step 2:- Create Apex Controller : opportunityHandlerCtrl.apxc
SFDX:Create Apex Class >> New >> opportunityHandlerCtrl.apxc
opportunityHandlerCtrl.apxc [Apex Class]
public class opportunityHandlerCtrl {
public static void updateOpportunity(List<Opportunity> oppList){
Set<Id> setId = NEW Set<Id>();
Map<Id, Decimal> accmap = NEW Map<Id, Decimal>();
DECIMAL mydecval = 0;
FOR(Opportunity oppObj : oppList){
setId.add(oppObj.AccountId);
}
system.debug('setId ' + setId);
List<AggregateResult> agrObj =[SELECT AccountId, SUM(Booking_Amount__c) totalAmount FROM Opportunity WHERE AccountId=:setId GROUP BY AccountId];
FOR(Opportunity opt:oppList){
FOR(AggregateResult agr:agrObj){
mydecval = (DECIMAL)agr.get('totalAmount');
accmap.put((Id)agr.get('AccountId') ,(DECIMAL)agr.get('totalAmount'));
Account accItem = NEW Account();
accItem.Id=(Id)agr.get('AccountId');
accItem.Total_Booking_Revenue__c = mydecval;
//system.debug('accmap44 ' + accItem);
//system.debug('keySet111 ' + accmap.keySet());
// system.debug('keyVal ' + accmap.values());
//
Campaign cpm = NEW Campaign();
cpm.Id = opt.CampaignId;
cpm.ActualCost=mydecval;
system.debug('cpm22 ' + cpm);
UPDATE cpm;
UPDATE accItem;
}
}
system.debug('mydecval!! ' + mydecval);
}
}
Further post that would you like to learn in Salesforce
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
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 |