You need to sign in to do that
Don't have an account?
Kiran 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.
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;
}
}
if it is ok plz make solavble........
All Answers
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
The amounttask Trigger shows the fallowing error message
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
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;
}
}
if it is ok plz make solavble........
Thanks friend.
Is this code right or wrong !
check it once please :-)