+ Start a Discussion
shaker shaikhshaker shaikh 

want update account fields by opportunity updates

Hi experts,
i want update account field from opportunity and have written below code block a class for account and a trigger which will invoke while inserting 
i have 

public class Account_opp_update_DAO {
    public static account opportunityInsert(id accountId, decimal newAmount){
        system.debug('called Account_opp_update_DAO.opportunityInsert of account id :'+accountId);
        system.debug('called Account_opp_update_DAO.opportunityInsert of newAmount id :'+newAmount);
        account act=[SELECT All_Opportunity_Revenue__c,Total_opportunity__c FROM Account where id=:accountId LIMIT 1];
        act.Total_opportunity__c= act.Total_opportunity__c+1;
        act.All_Opportunity_Revenue__c=act.All_Opportunity_Revenue__c+newAmount;
        return act;
    }
}

trigger

trigger Opportunity_detail on Opportunity (after insert,after update, after delete, after undelete) {
    list <Opportunity> lstopp = new list<opportunity>();
    list <Account> lstAccount = new list<Account>();
    if (trigger.isInsert){
        for(opportunity op: lstopp){
            system.debug(op);
            system.debug(op.AccountId);
            Account act = Account_opp_update_DAO.opportunityInsert(op.accountID, op.amount);
            lstAccount.add(act);
       }    
            insert lstAccount;
            update lstopp;
    }
}
code doesn't give any error but i unable to update account field

My Requirment
On account field there are two field Total_opportunity__C and Total_revenue_from_all_opp__C
 when ever new opportunity is created want that should be appear on account page and if there are multiple opportunity connected with account
so that total summary should show in Total_revenue_from_all_opp__C.

im going by creating DAO class on account. i have searched other way that we can directly creat trigger on opportunity, but didnot understand

please help to get this solved

Thanks.
srlawr uksrlawr uk
You almost definitely don't want to use a trigger for this?! Can you not use Roll Up Summary fields...

Have a look at creating Roll Up summary fields on Opportunity, you can just make your Total_opportunity__C a COUNT of the number of child Opportunities on the Account, and then Total_revenue_from_all_opp__C is no more than a SUM summary field on Account of the child Opportunity revenues?

User-added image

That should take you about 2 minutes to do. 

The code above would take hours to correctly produce, test and promote (and trust, me I am a huge code advocate, but this is a classic declarative scenario).

You also don't want the "update lstopp;" bit in your trigger, and lstopp is never being assigned to anything (which would be one of the trigger context variables), so your current trigger is literally doing nothing - but also will have a recursion issue (I'm almost suprised it compiled). I really don't think coding this is your best choice! Just use Roll Up Summary fields!
 
shaker shaikhshaker shaikh
Thank you srlawr,
 yes this is the best way to show count in case masterdetail relationship i am practicing it on lookup relattionship.
 in this case there are multiple DML  will happen like - if opportunity get deleted that should be update,incase of undelete , or opportunity insertation and update 
Do i get all DML cover in roll up summary field..?