function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JAGJOT SINGHJAGJOT SINGH 

Calculate sum of Amount field

I want to create a custom field that calculates the sum of all 'Amount' opportunity fields for same opportunity, so that I can bucket this field in my report. It seems that it cannot bucket if I just summarize the Amount field, so I'm assuming if I create a custom Amount Sum field, this would work.

So for one account, I have 3 same updated opportunities:

Bucket - Total Opportunities 3 or more : $500 - $1000
Org: John Smith
        Amount: $100
        Amount: $100
        Amount: $400
       >>New Sum Field: $600

How would I create this in apex class?
Best Answer chosen by JAGJOT SINGH
Harshit Garg 6Harshit Garg 6
HI Jagjot,

Please use below code.
 
Count Total opportunities amount on account.
     First create one custom field on Account object = total_opp_amount (datatype = number);

// Trigger

trigger totaloppamount on opportunity(after insert,after update,after delete){
     map<id,list<opportunity>> accidopplistmap  = new map<id,list<opportunity>>();
     set<id> accids = new set<id>();
     list<opportunity> opplist = new list<opportunity>();
       if(trigger.isinsert || trigger.isupdate){
         for(opportunity opp : trigger.new){
           if(opp.accountid != null){
          accids.add(opp.accountid);
         }
        }
        }
       if(trigger.isdelete){
         for(opportunity opp : trigger.old){
         if(opp.accountid != null){
            accids.add(opp.accountid);
         }
         }
       }   
     if(accids.size()>0){
        opplist = [select amount,Accountid from opportunity where Accountid in : accids];
        for(opportunity op : opplist){
           if(op.amount != null){
           if(!accidopplistmap.containskey(op.accountid)){
                accidopplistmap.put((op.accountid),new list<opportunity>());
           }
           }
               accidopplistmap.get(op.accountid).add(op);
        }
       list<account> acclist = new list<account>();
       acclist = [select total_opp_amount__c from account where id in :accids];
       for(account acc: acclist){
          list<opportunity> templist = new list<opportunity>();
          templist = accidopplistmap.get(acc.id);
          Double totalopamount = 0;
           for(opportunity o: templist){
            if(o.Amount!= null){
               totalopamount += o.Amount;
            }
          }
          acc.total_opp_amount__c = totalopamount;
        }
        update acclist;
     } 

     }

That above trigger definitely will hep you. Please choose my ans as the best ans.

Thanks,
Harshit Garg
 

All Answers

Harshit Garg 6Harshit Garg 6
HI Jagjot,

Please use below code.
 
Count Total opportunities amount on account.
     First create one custom field on Account object = total_opp_amount (datatype = number);

// Trigger

trigger totaloppamount on opportunity(after insert,after update,after delete){
     map<id,list<opportunity>> accidopplistmap  = new map<id,list<opportunity>>();
     set<id> accids = new set<id>();
     list<opportunity> opplist = new list<opportunity>();
       if(trigger.isinsert || trigger.isupdate){
         for(opportunity opp : trigger.new){
           if(opp.accountid != null){
          accids.add(opp.accountid);
         }
        }
        }
       if(trigger.isdelete){
         for(opportunity opp : trigger.old){
         if(opp.accountid != null){
            accids.add(opp.accountid);
         }
         }
       }   
     if(accids.size()>0){
        opplist = [select amount,Accountid from opportunity where Accountid in : accids];
        for(opportunity op : opplist){
           if(op.amount != null){
           if(!accidopplistmap.containskey(op.accountid)){
                accidopplistmap.put((op.accountid),new list<opportunity>());
           }
           }
               accidopplistmap.get(op.accountid).add(op);
        }
       list<account> acclist = new list<account>();
       acclist = [select total_opp_amount__c from account where id in :accids];
       for(account acc: acclist){
          list<opportunity> templist = new list<opportunity>();
          templist = accidopplistmap.get(acc.id);
          Double totalopamount = 0;
           for(opportunity o: templist){
            if(o.Amount!= null){
               totalopamount += o.Amount;
            }
          }
          acc.total_opp_amount__c = totalopamount;
        }
        update acclist;
     } 

     }

That above trigger definitely will hep you. Please choose my ans as the best ans.

Thanks,
Harshit Garg
 
This was selected as the best answer
JAGJOT SINGHJAGJOT SINGH

Thankew So Much Sir....

 

 

Jagjot Singh