+ Start a Discussion
pranav shah 40pranav shah 40 

Opportunity Update Trigger on Account object





Hi All,,

please find the below code. as i am trying to update a field (Total Opportunity Amount) on Account object whenever opportunity ia
added, if one opportunity is moved from one Account to another account.. other account field is getting updated, but the value is not getting subtracted from previous one

please check my else block and let me know what issue i am facing...please dont  write an code.... just expalin me logic what can i do.. so that i can learn and try myself


public void opportunityownerchange(list<opportunity> opps,map<id,opportunity> mapopportunity)
    {
        set<id> opt=new Set<id>();
        for(opportunity opp:opps)
        {
         opt.add(opp.Accountid);
        }
        List<Account> lstAcct =[select id,Name,Total_Opportunity_Amount__c,(select id, Amount from opportunities) from account where Id IN:opt];
        for(Account acc:lstAcct)
        {
        for(Opportunity opp:opps)
        {
            double TotalAmount=0;
            Opportunity oldopp = mapopportunity.get(opp.Id);
            if(oldopp.AccountID!=opp.AccountID && opp.AccountID!= null )
            {
            if(opp.Amount!=null)
            {  
              totalAmount=TotalAmount+opp.Amount;
            }
            else
            {
             totalAmount= totalAmount-opp.Amount;
            }
            }  
            acc.Total_Opportunity_Amount__c=totalAmount;
            lstacct.add(acc);
        }
        }
        update lstacct;
    }
}
RamakantRamakant
Hi Pranav,

In this case your code should work for all events: After Insert, After Update, After Delete and after Undelete.
Because you can create new opportnity under Account, Can Update Account Field on Opportunity, Can delete Opportunity from the system and Can Undelete deleted Opportunity.
As you are moving opportunity from one account to other update happens on Opporunity but your Account is not aware about this update and value in the desired field remains same . so we need to update Account in this scenario.

PLease mark as best answer if helped.

Regards,
Ramakant
Dev_AryaDev_Arya
Hi Pranav,

First of all, I appreciate that you mentioned you want to know the logic and error in your code rather than seeking for the code. 
Secondly, with respect to your code, I would like to mention couple of points:
1. If you only intend to change the total amount of the opp at the Accoubt level, this can very easily be managed using Trigger.new and Trigger.old (assuming you are also calling your above method also from the trigger)
2. In the first line, you made a set of accountIds', where if I am not wrong 'opps' is the list of updates opportunities (Trigger.new), so in principle, when you made a select query, you never searched for the old account, and never added to the update list.
3. I would really advice, please use a good naming convention, this will be one of the very primary things of a good programmer.

Feel free to shot any question. WIll be happy to answer.

Cheers, Dev