Skip to main content
Hi Expert,

I have a account with oppertunities. now  i want to add GrossTotal custom field in Account and all opertunity Amount will be add in Gross total.

Please suggest.

Regards

Romesh

  

 
2 answers
  1. Dec 14, 2017, 10:26 AM
    Hi Romesh,

    Please use below code

     

    trigger GrossAmountRollUp on Opportunity (after insert, after update) {

        List<Account> accList = new List<Account>();

        List<Id> listIds = new List<Id>();

        Decimal grossTotal = 0;

        

        for(Opportunity opp: Trigger.new){

            listIds.add(opp.AccountId);

        }

        

     for(Account a: [select Name,(select id, Amount from opportunities) from Account  where id =:listIds])

        {   

           for(Opportunity o:a.opportunities){

              grossTotal  = grossTotal+o.Amount;

            }

            a.dotsquaremukesh__Gross_Amount__c = grossTotal;

            accList.add(a);

                

        }

        

        update accList;

    }

    Regards

    Mukesh
0/9000