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
Kiran Kumar GottulaKiran Kumar Gottula 

trigger on opportunity(updating a field in account)

Hi Every one,

                         In my account object  "Total Amount" field is there,

                         and in opportunity "amount" field  is there,

                         

                        I want to dispaly sum of amount of all child  opportunities in the total amount field in the parent account.

 

                       So please help me, how to write trigger for that.

Best Answer chosen by Admin (Salesforce Developers) 
PremanathPremanath

I have changed it .. If Total amount is null it's giving error..

so you can use this code

 

trigger AmountTask on Opportunity (after insert,after update,after delete){
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){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
System.debug('++++++++++opp.accountid--------finally---->'+a.total_amount__c);
}
update alist;
}
if(Trigger.isUpdate){
Map<id,decimal> acctotal = new Map<id,decimal>();
List<Opportunity> oppOld = Trigger.old;
for(opportunity opp :trigger.New){
if(opp.amount!=null){
acctotal.put(opp.accountid,opp.amount);
}
}
Map<id,decimal> acc = new Map<id,decimal>();
for(opportunity oppod :trigger.old){
if(oppod.amount!=null){
acc.put(oppod.accountid,oppod.amount);
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c+=acctotal.get(a.id)-acc.get(a.id);
alist.add(a);
}
update alist;
}
if(Trigger.isDelete){
List<Opportunity> oppOld = Trigger.old;
Map<id,decimal> acc = new Map<id,decimal>();
for(opportunity oppod :trigger.old){
if(oppod.amount!=null){
acc.put(oppod.accountid,oppod.amount);
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acc.keyset()];
for(account a:acclist){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c-=acc.get(a.id);
alist.add(a);
}
update alist;
}
}


Nani224 wrote:

The amounttask Trigger shows the fallowing error message

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AmountTask caused an unexpected exception, contact your administrator: AmountTask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AmountTask: line 21, column
1
  
Please resolve this error
 

if it is ok plz make solavble........

 

All Answers

PremanathPremanath

Hi 

You can do it with rollup summary also..

 

If you want trigger..

 

 

trigger AmountTask on Opportunity (after insert,after update,after delete){
  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;
  }
 if(Trigger.isUpdate){
      Map<id,decimal> acctotal = new Map<id,decimal>();
      List<Opportunity> oppOld = Trigger.old;
        for(opportunity opp :trigger.New){
            if(opp.amount!=null){    
                    acctotal.put(opp.accountid,opp.amount);                                   
               }
        }
         Map<id,decimal> acc = new Map<id,decimal>();
        for(opportunity oppod :trigger.old){
            if(oppod.amount!=null){                       
                    acc.put(oppod.accountid,oppod.amount);                  
               }
        }
        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)-acc.get(a.id);
            alist.add(a);
        }
        update alist;
   }
   if(Trigger.isDelete){
        List<Opportunity> oppOld = Trigger.old;
        Map<id,decimal> acc = new Map<id,decimal>();
        for(opportunity oppod :trigger.old){
            if(oppod.amount!=null){                       
                    acc.put(oppod.accountid,oppod.amount);                  
               }
        }
        list<account> alist= new list<account>();
        list<account> accList = [select total_amount__c from account where id in:acc.keyset()];
        for(account a:acclist){
            a.total_amount__c-=acc.get(a.id);
            alist.add(a);
        }
        update alist;
   }
}

 

 

 

 

If  it is helpful plz make solvable others it may benfit

Kiran Kumar GottulaKiran Kumar Gottula

The amounttask Trigger shows the fallowing error message

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AmountTask caused an unexpected exception, contact your administrator: AmountTask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AmountTask: line 21, column
1
  
Please resolve this error
 
PremanathPremanath

I have changed it .. If Total amount is null it's giving error..

so you can use this code

 

trigger AmountTask on Opportunity (after insert,after update,after delete){
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){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
System.debug('++++++++++opp.accountid--------finally---->'+a.total_amount__c);
}
update alist;
}
if(Trigger.isUpdate){
Map<id,decimal> acctotal = new Map<id,decimal>();
List<Opportunity> oppOld = Trigger.old;
for(opportunity opp :trigger.New){
if(opp.amount!=null){
acctotal.put(opp.accountid,opp.amount);
}
}
Map<id,decimal> acc = new Map<id,decimal>();
for(opportunity oppod :trigger.old){
if(oppod.amount!=null){
acc.put(oppod.accountid,oppod.amount);
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c+=acctotal.get(a.id)-acc.get(a.id);
alist.add(a);
}
update alist;
}
if(Trigger.isDelete){
List<Opportunity> oppOld = Trigger.old;
Map<id,decimal> acc = new Map<id,decimal>();
for(opportunity oppod :trigger.old){
if(oppod.amount!=null){
acc.put(oppod.accountid,oppod.amount);
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acc.keyset()];
for(account a:acclist){
if(a.total_amount__c==null){
a.total_amount__c=0;
}
a.total_amount__c-=acc.get(a.id);
alist.add(a);
}
update alist;
}
}


Nani224 wrote:

The amounttask Trigger shows the fallowing error message

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AmountTask caused an unexpected exception, contact your administrator: AmountTask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AmountTask: line 21, column
1
  
Please resolve this error
 

if it is ok plz make solavble........

 

This was selected as the best answer
Kiran Kumar GottulaKiran Kumar Gottula

Thanks friend.

Kiran Kumar GottulaKiran Kumar Gottula

Is this  code right or wrong !

 

check it once please :-)

 

trigger amounttask on Opportunity (after delete, after insert, after undelete, 
after update) {
 
list<Account> lstacc=new List<Account>();
set<id> accids=new set<id>();
Map<id,double> mapacc=new map<id,double>();
if(trigger.isInsert || trigger.isupdate){
for(opportunity opp:Trigger.new){
if(opp.Accountid!=null){
accids.add(opp.Accountid);
}
 
}
}
if(TRigger.Isdelete || Trigger.isupdate){
for(opportunity opp:trigger.old){
if(opp.Accountid!=null){
accids.add(opp.Accountid);
}
}
 
}
if(accids!=null && accids.size()>0){
for(AggregateResult ar:[select Accountid accid,Sum(Amount) totamount from opportunity where accountid in:accids Group By Accountid]){
mapacc.put((id)ar.get('accid'),(double)ar.get('totamount'));
}
}
for(Account acc:[select id,opportunity_amount__c from Account where id in:accids]){
if(mapacc.get(acc.id)!=null){
double total=mapacc.get(acc.id);
acc.Total_Amount__c=total;
lstacc.add(acc);
}
else{
acc.Total_Amount__c=0;
lstacc.add(acc);
}
}
 
 
system.debug('Acc list--->'+lstacc);
if(lstacc!=null && lstacc.size()>0){
update lstacc;
}
}