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
belabela 

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;
    } 
FearNoneFearNone
hi mahanandeesh,

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
Account acc = ma.get(opp.accountid);
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:
for (Account oneAf : af) {
    for(integer i =0;i<oneAf.opportunities.size();i++){
    ...
    }
}

I hope can help...

 
belabela
Hi FearNone,

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.
belabela
Hi FearNone,

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
FearNoneFearNone
hi mahanandeesh,

i guess is that, ac.total_opportunity_amount__c has no value.
try setting ac.total_opportunity_amount__c-field to required.

User-added image


Best Regards
belabela
Hi fearnone,
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,
FearNoneFearNone
that's because "integer i" is for opportunities, and not for "af".
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".
system.debug('hiiiiiii'+af);
    
for(account ac:af){
        ac.total_opportunity_amount__c = ac.total_opportunity_amount__c+ opp.amount ;
}                  
update af;

HOWEVER, you need to add a condition bertween a NEW record or an UPDATE record.
 
FearNoneFearNone
I modified your code to meet your requirements. though this is not tested, and it's just for your reference.
trigger addoppamount  on opportunity(after update,after insert) {
   
    set<id> s=new set<id>();
    
    for (opportunity op:trigger.new){
        s.add(op.accountid);
    }
    
    for (opportunity op:trigger.old){
        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]);

    for(opportunity opp:trigger.new){
        Account acc = ma.get(opp.accountid);
        if (acc != null){
            acc.total_opportunity_amount__c = acc.total_opportunity_amount__c+ opp.amount ;
        }
        //in case of update, minus old value
        if (Trigger.IsUpdate){
            opportunity old_opp = Trigger.oldMap.get(opp.Id);
            Account old_acc = ma.get(old_opp.accountid);
            if (acc != null && old_opp.accountid == opp.accountid){
                // update the account
                acc.total_opportunity_amount__c = acc.total_opportunity_amount__c - old_opp.amount;
            } else if(old_acc != null){
                // update the old account
                old_acc.total_opportunity_amount__c = old_acc.total_opportunity_amount__c - old_opp.amount;
            }
        }

    }    
    update ma.values();
}


Best Regards!
belabela
Hi Fearnone,
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,