function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PremanathPremanath 

How to write same code for update operation

Hi all,

I have a requirement , Add all opportunity Amount fields , display total amount in Account object.

i have code it's working for inserting a record in opportunity.

But update and delete operation does not work.

i know through Roll-up summary we can but i want it from trigger only please help me ...........

 

 

trigger AmountTask on Opportunity (after insert,after update){
  if(Trigger.isinsert){
        Map<id,decimal> acctotal = new Map<id,decimal>();
        for(opportunity opp :trigger.new){
            if(opp.amount!=null){    
                if(acctotal.get(opp.accountid)==null){
                    acctotal.put(opp.accountid,opp.amount);
                    System.Debug('++++++opp.accountid--------Null--->'+acctotal.get(opp.accountid));
                }
                else
                {
                    decimal amt = 0;
                    amt = acctotal.get(opp.accountid)+opp.amount;
                    acctotal.put(opp.accountid,amt);
                    System.debug('++++++++++opp.accountid--------NotNull--Total Amount---->'+acctotal.get(opp.accountid));
                    
                }
            }
        }
        list<account> alist= new list<account>();
        list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
        for(account a:acclist){
            a.total_amount__c+=acctotal.get(a.id);
            alist.add(a);
            System.debug('++++++++++opp.accountid--------finally---->'+a.total_amount__c);
        }
        update alist;
  }

}

 

 

plz help me thank you guys,

 

Rahul SharmaRahul Sharma

Hi Premanath,

 

You have added following check in your code,

if(Trigger.isinsert){

Due to which your Trigger is limited to work for only insert case, However you have to include all the necessary events in your trigger for which you need to perform action.

PremanathPremanath

Hi ,

 

yes for that pupous only i have posted my problm.

I don't know how to work with this for update operation.

please make it.

 

Thank you,

PremanathPremanath

Hi guys,

No need any help , I have done it in my org.

 

Thank you