Apex Trigger Whenever Opportunity is: Inserted/Updated/Deleted/Undeleted We need to: Calculate total Amount of all Opportunities Where StageName = ‘Closed Won’ And update it on Account
Step 1 Create Field on Account
Create Currency field:
| Field Label | API Name |
|---|---|
| Total Revenue | Total_Revenue__c |
Data Type β Currency
Logic (Same Clean Pattern)
- Collect AccountIds
- Run ONE Aggregate SOQL using SUM(Amount)
- Store result in Map
- Update Accounts
Step 2 Opportunity Trigger (After Update): OpportunityRevenueSumTrigger.apxt
TRIGGER OpportunityRevenueSumTrigger ON Opportunity
(after INSERT, after UPDATE, after DELETE, after undelete) {
Set<Id> accountIds = NEW Set<Id>();
// Collect AccountIdsIF(TRIGGER.isInsert || TRIGGER.isUpdate || TRIGGER.isUndelete){
FOR(Opportunity opp : TRIGGER.new){
IF(opp.AccountId != NULL){
accountIds.add(opp.AccountId);
}}}IF(TRIGGER.isDelete){
FOR(Opportunity opp : TRIGGER.old){
IF(opp.AccountId != NULL){
accountIds.add(opp.AccountId);
}}}IF(accountIds.isEmpty()){
RETURN;}// Map TO store total revenue
Map<Id, Decimal> revenueMap = NEW Map<Id, Decimal>();
// Aggregate Query USING SUM()
List<AggregateResult> results = [
SELECT AccountId, SUM(Amount) totalAmount
FROM OpportunityWHERE AccountId IN :accountIds
AND StageName = 'Closed Won'
GROUP BY AccountId
];// Store RESULT IN map
FOR(AggregateResult ar : results){
Id accId = (Id) ar.get('AccountId');
DECIMAL totalRevenue = (DECIMAL) ar.get('totalAmount');
revenueMap.put(accId, totalRevenue);
}List<Account> accountList = NEW List<Account>();
// UPDATE Accounts
FOR(Id accId : accountIds){
Account acc = NEW Account();
acc.Id = accId;
acc.Total_Revenue__c =
revenueMap.containsKey(accId) ? revenueMap.get(accId) : 0;
accountList.add(acc);
}IF(!accountList.isEmpty()){
UPDATE accountList;}}
Why This Is Best Practice?
- Only ONE SOQL
- Uses SUM() Aggregate function
- No SOQL inside loop
- No DML inside loop
- Handles Insert, Update, Delete, Undelete
- Bulk Safe (200 records)
- Production-ready pattern
How do I see all opportunities in Salesforce?
In most cases, your list view defaults to Recently Viewed. To toggle to a different list view, click the down arrow next to the list view name and select All Opportunities.
What is the bulkifying trigger in Salesforce?
The benefit of bulkifying your code is that bulkified code can process large numbers of records efficiently and run within governor limits on the Salesforce Platform. These governor limits are in place to ensure that runaway code doesn't monopolize resources on the multitenant platform.
What is the opportunity amount in Salesforce?
For Opportunities with Products, the amount is the sum of the related Products. You cannot directly edit the amount unless the Opportunity has no Products. To change the amount for an Opportunity that contains Products, edit the Sales Price or Quantity of the related Products.
Related Topics | You May Also Like
|
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β |


