+ Start a Discussion
mohan s 37mohan s 37 

How to add opportunity amount to associated account annual revenue automatically and gets remove opportunity amount from account automatically whenever the the opportunity is deleted

I want to add opportunity Amount to related account AnnualRevenue automatically and i want to remove the Opportunity amount from Account AnnualRevenue when opportunity gets deleted. And I want to update Account AnnualRevenue with updated opportunity amount.To achieve this i am getting following error "Invalid foreign key relationship: Account.Opportunities" could any one please resove this issue.I hope I will get correct code.

This is my trigger:
trigger addoppAmounttoAcct on Opportunity (After insert, After update) {
    public decimal amount=0;
    Map<id,integer> oppmap=new Map<id,integer>();
    List<Account> acc=new List<Account>();
    List<Account> a=[SELECT Id,Name,AnnualRevenue,(SELECT Id,Name, Amount FROM Opportunities) FROM Account];
    if(trigger.isInsert){
    set<Id> oppids=trigger.newMap.keySet();
    Map<Id,Integer> accmap=new Map<Id,Integer>();
    //List<Account> a=[SELECT Id,Name,AnnualRevenue,(SELECT Id,Name, Amount FROM Opportunities) FROM Account];
    List<Opportunity> opplist=[SELECT Id, Name,Amount,Opportunity.AccountId FROM Opportunity WHERE Id IN:oppids];
    //List<Account> acc=new List<Account>();
    for(Account ac:a){
    for(Opportunity o:opplist){
        if(o.AccountId==ac.id){
          ac.AnnualRevenue=ac.AnnualRevenue+o.Amount;
            acc.add(ac);
       }else{
           oppmap.put(o.Id, 1); 
        }
     }
  }
    update acc;
        }
 if(trigger.isUpdate){
   set<Id>updateids=trigger.newMap.keySet();
     List<Opportunity> opplistids=[SELECT Id, Name, Opportunity.AccountId,Opportunity.Account.AnnualRevenue, Amount FROM Opportunity WHERE Id IN:updateids];
        for(Opportunity upopp:opplistids){
            for(Account aid:a){
            if(upopp.AccountId==aid.Id){
                amount=upopp.Amount;
                aid.AnnualRevenue=aid.AnnualRevenue-aid.Opportunities.Amount;// here i am getting error like "Invalid foreign key relationship: Account.Opportunities"
                amount=aid.AnnualRevenue;
              aid.AnnualRevenue=amount+upopp.Amount;
                acc.add(aid); 
                }
            } 
        }
        update acc;
    }

Here my intension is to add opportunity amount to account Annual revenue after inserting the opportunity. And update the Annual revenue with updated  opportunity Amount. when i try to inserting opportunity there is no issue. When i try to perform update opportunity amount the previous amount and updated amount both will be added to the account Annualrevenue but my intension is to update only current updated Amount and remove previous amount from the Account AnnualRevenue.
Example:
i have one Account i.e' account1' if Icreate opportunity i.e 'account1opportunity' for this account with amount as 2000 the account1 Annual revenue is updated as 2000 this is fine, but if update account1opportunity with 4000 the related account1 AnnualRevenue is updated with previous amount i.e, 2000 plus updated Amount 4000 i.e  Annual revenue will be updated as 6000 according to my code. But I want to remove previous 2000 amount and update AnnualRevenue with current updated Amount i.e Annual revenue should be 4000.
sandeep reddy 37sandeep reddy 37
use master detailed field and also use roll up summery  u can achive it 
 
Dilip_VDilip_V
Mohan,

1.Just create roll up summery field(SUM) on account.

Let us know if you have any issues.

Mark it as best answer if it works.
Thanks.