You need to sign in to do that
Don't have an account?
bela
can any one help me in this trigger,I am writting a trigger,which is displaying amount of opportunities of account
I tried in below way,but i am getting this error execution of AfterInsert caused by: System.NullPointerException: Argument cannot be null.: Trigger.addoppamount: line 18, column 1 and also getting another error at line 16 line ,can any one explain where i did mistake.
trigger addoppamount on opportunity(after update,after insert) {
set<id> s=new set<id>();
for (opportunity op:trigger.new){
s.add(op.accountid);
}
map<id,account> ma =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id from opportunities) from account where id in :s]);
for(opportunity opp:trigger.new){
Account acc = new Account();
acc.id=ma.get(opp.accountid).id;
list<account> af= [select id,name,total_opportunity_amount__c,(select id from opportunities) from account where id =: acc.id];
system.debug('hiiiiiii'+af);
for(integer i =0;i<af.oppotunities.size();i++)//In for loop it is not accepting af.oppotunities.size() and giving Initial term of field expression must be a concrete SObject: List<Account> at line 16 column 28 but it is taking like this i<af.oppotunities.size(),can any one explain why
{
acc.total_opportunity_amount__c =opp.amount + acc.total_opportunity_amount__c;//18th line Here it is showing error
system.debug('hiiiiiii'+acc.total_opportunity_amount__c);
}
update acc;
}
}
trigger addoppamount on opportunity(after update,after insert) {
set<id> s=new set<id>();
for (opportunity op:trigger.new){
s.add(op.accountid);
}
map<id,account> ma =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id from opportunities) from account where id in :s]);
for(opportunity opp:trigger.new){
Account acc = new Account();
acc.id=ma.get(opp.accountid).id;
list<account> af= [select id,name,total_opportunity_amount__c,(select id from opportunities) from account where id =: acc.id];
system.debug('hiiiiiii'+af);
for(integer i =0;i<af.oppotunities.size();i++)//In for loop it is not accepting af.oppotunities.size() and giving Initial term of field expression must be a concrete SObject: List<Account> at line 16 column 28 but it is taking like this i<af.oppotunities.size(),can any one explain why
{
acc.total_opportunity_amount__c =opp.amount + acc.total_opportunity_amount__c;//18th line Here it is showing error
system.debug('hiiiiiii'+acc.total_opportunity_amount__c);
}
update acc;
}
}
https://developer.salesforce.com/docs/atlas.en-us.fundamentals.meta/fundamentals/adg_relationships_roll_up_sum_fields_try_it_out.htm
1. About the NullPointerException:
- you instantiate a new Account() and then set its ID (acc.id=ma.get(opp.accountid).id;), therefore acc.total_opportunity_amount__c will always be null. You can fix this as BUT! be sure account in opportunity must be set or it will NULL-error again.
2. About the SObject: List<Account>:
- you have a LIST of af and you can't simply do af.opportunities.
So you have to loop af first:
I hope can help...
Thanks for your response,It is very helpful,but I tried with below code ,I am still getting following exception.can you please help in this trigger
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger addoppamount caused an unexpected exception, contact your administrator: addoppamount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.addoppamount: line 23, column 1
trigger addoppamount on opportunity(after update,after insert) {
set<id> s=new set<id>();
for (opportunity op:trigger.new){
s.add(op.accountid);
}
map<id,account> ma =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id,amount from opportunities) from account where id in :s]);
system.debug('mapppppppppp'+ma);
for(opportunity opp:trigger.new){
Account acc = ma.get(opp.accountid);
system.debug('hiiiiiiiacccccccccc'+acc );
list<account> af= [select id,name,total_opportunity_amount__c,(select id,amount from opportunities) from account where id =:acc.id];
system.debug('hiiiiiii'+af);
for(account ac:af)
{
for(integer i=0;i<ac.opportunities.size();i++){
system.debug('hiiiiiii1111'+ac.total_opportunity_amount__c);
ac.total_opportunity_amount__c = ac.total_opportunity_amount__c+ opp.amount ;23 line addoppamount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.addoppamount: line 23, column 1
system.debug('hiiiiiii2'+ac.total_opportunity_amount__c);
}
}
update acc;
}
Thanks,
B.Mahanandeesh.
I tried debugging,I am getting null value for ac.total_opportunity_amount__c ,I am not undersatanding why i am getting null,please help me system.debug('hiiiiiii1111'+ac.total_opportunity_amount__c);
Thanks,
B.Mahanandeesh
i guess is that, ac.total_opportunity_amount__c has no value.
try setting ac.total_opportunity_amount__c-field to required.
Best Regards
thanks for response, It worked but i am getting error like System.ListException: List index out of bounds: 1: Trigger.addoppamount: line 27, column 1,can you please help me and explain why i am getting this error,It is taking first record amount but not taking second record amount
trigger addoppamount on opportunity(after update,after insert) {
set<id> s=new set<id>();
for (opportunity op:trigger.new){
s.add(op.accountid);
}
List<account> ll=new List<account> ();
map<id,account> ma =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id,amount from opportunities) from account where id in :s]);
system.debug('mapppppppppp'+ma);
for(opportunity opp:trigger.new){
Account acc = ma.get(opp.accountid);
system.debug('hiiiiiiiacccccccccc'+acc );
list<account> af= [select id,name,total_opportunity_amount__c,(select id,amount from opportunities) from account where id =:acc.id];
system.debug('hiiiiiii'+af);
for(account ac:af){
if(ac.opportunities.size()>0){
for(integer i=0;i<ac.opportunities.size();i++){ //
system.debug('hiiiiiii1111'+ac.total_opportunity_amount__c);
ac.total_opportunity_amount__c = af[i].total_opportunity_amount__c+ opp.amount ;//System.ListException: List index out of bounds: 1: Trigger.addoppamount: line 27, column 1,
system.debug('hiiiiiii2'+ac.total_opportunity_amount__c);
ll.add(ac);
}
update ll;
}
}
}
}
Thanks,
B.Mahanandeesh,
Do you want to add the opp.amount to Account "ac"?
then you don't need the i-loop.
you also don't need the "ll"list because you already have the "af".
HOWEVER, you need to add a condition bertween a NEW record or an UPDATE record.
Best Regards!
Thanks for your valuble information,based on the given suggestions i have done the coding in below way its working fine.but i have small doubt,can you please help to solve it.
doubt 1::In 29 th line In this line i kept update acc ,It is not updating accounts untill i kept update ma.values(); ,where as when i use update ac for forloop code which is kept in comments its working fine.
doubt 2::The same we are updatng for update and delete by using update a and update accc its working fine,why not while inserting it is not updating the record
trigger addoppamount on opportunity(after update,after insert,after delete) {
if(!trigger.isDelete){
set<id> s=new set<id>();
for (opportunity op:trigger.new){
s.add(op.accountid);
}
map<id,account> ma =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id,amount from opportunities) from account where id in :s]);
list<account> af= [select id,name,total_opportunity_amount__c,(select id,amount from opportunities) from account where id in:s];
if(Trigger.isInsert){
for(opportunity opp:trigger.new){
Account acc =ma.get(opp.accountid) ;
system.debug('hiiiiiiiacccccccccc'+acc );
system.debug('hiiiiiii'+af);
if(opp.accountid!=null){
acc.total_opportunity_amount__c = acc.total_opportunity_amount__c+ opp.amount ;
}
/*for(account ac:af){
system.debug('hiiiiiii1111'+ac.total_opportunity_amount__c);
ac.total_opportunity_amount__c = ac.total_opportunity_amount__c+ opp.amount ;
system.debug('hiiiiiii2'+ac.total_opportunity_amount__c);
}
update ac; */
}
update ma.values(); //29 th line In this line i kept update acc ,It is not updating accounts untill i kept update ma.values(); ,where as when i use update ac for forloop code which is kept in comments its working fine
}
if(Trigger.isUpdate ){
set<id> os=new set<id>();
for(opportunity op:trigger.old){
os.add(op.accountid);
}
map<id,account> moa =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id,amount from opportunities) from account where id in :os]);
for(opportunity opp:trigger.new){
account a=ma.get(opp.accountid);
//list<account> af= [select id,name,total_opportunity_amount__c,(select id,amount from opportunities) from account where id =:a.id];
opportunity op=trigger.oldmap.get(opp.id);
account oa=moa.get(op.accountid);
if(op.accountid == opp.accountid ){
system.debug('update in for');
a.total_opportunity_amount__c=a.total_opportunity_amount__c-op.amount+opp.amount;
}
update a;//doing update here
}
}
}
if(Trigger.isDelete){
set<id> od=new set<id>();
for(opportunity op:trigger.old){
od.add(op.accountid);
}
map<id,account> moa =new map<id,account>([select id,name,total_opportunity_amount__c ,(select id,amount from opportunities) from account where id in :od]);
for(opportunity opp:trigger.old){
account accc=moa.get(opp.accountid);
accc.total_opportunity_amount__c= accc.total_opportunity_amount__c-opp.amount;
update accc;//doing update here
}
}
Thanks,
B.Mahanandeesh,