+ Start a Discussion
sfquestiondevsfquestiondev 

Sum opportunity amounts at one account

Hi,

 

Can someone help me in plain english and step by step about this problem:

 

I have one account with five opportunities attached to its account.

At account i have a field total_amount__c

 

I need a trigger code that will sum all amounts from this five opportunities and update total_amount__c field with sum result.

 

Please if enione can help me

 

Thank you!

kiranmutturukiranmutturu

for this scenario u can go with roll up summary...

create a roll up summary on account select the object ti summarize as opportunity and select the sum and sleect the field as amount ......if you need any filter criteria on the opportunites .. then u can go for some opportunites based on filter criteria other wise default it will take all opportunites amount only.....got it?

sfquestiondevsfquestiondev

I know for that, but I need trigger. I will have to modify it with different "where", "limit" options

kiranmutturukiranmutturu

tell me requirement clearly then....

sfquestiondevsfquestiondev

Hi kiran_mutturu, here are details of my trigger:

 

I have one account with five opportunities attached to its account.

At account i have a field total_amount__c

 

I need a trigger code that will sum amounts just from the last two opportunities (the 4. and the 5. It is order by amount asc) and update total_amount__c field with sum result.

Andy BoettcherAndy Boettcher

Here's a stab...

 

Are you looking for a conditional rollup - like if you have 2 "type A" opportunities and 3 "type B" Opportunities, you are looking to have two fields on the Account object showing the SUMs for each "type"?  Secondly, when you update an Opportunity - you want a trigger to fire to update the parent Account?

 

(I use the term "type" purely for the sake of conversation)

 

 

sfquestiondevsfquestiondev

I have one account with five opportunities attached to its account.

At account i have a field total_amount__c

 

I need a trigger code that will sum all amounts from this five opportunities and update total_amount__c field with sum result.

 

Please if enione can help me

sfquestiondevsfquestiondev

Please, this is very urgent to me. I think that my question is very clear and very simple

asd@as.asdasd@as.asd

trigger sumOfAmount on Account (before update) {
    //AggregateResult[] aggOppList = [Select Account.Name acc,Sum(Amount)amount From Opportunity  GROUP BY Account.Name];
    map<id,double> ammount= new map<id,double>();
    for (AggregateResult results : [Select Account.id acc,Sum(Amount)amount From Opportunity  GROUP BY Account.id]){
        system.debug(results );
        ammount.put((ID)results.get('acc'),(double)results.get('amount'));
    }
    system.debug('#################### ' +ammount);
    
    list<Account> accList=new list<Account>();
    for(Account newAcc : trigger.new){
       if( ammount.containsKey(newAcc.id)){
           system.debug('@@@@@@@@@@@@@@@@@@ '+ ammount.get(newAcc.id));
           newAcc.Ammount__c=(double)ammount.get(newAcc.id);
           
       }
    }
   
}