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 opportunity of account in account field

can any one help me in this trigger,I am writting a trigger,which is displaying amount of opportunities of account in account field.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;
    }

    
    
      
}
sfdcMonkey.comsfdcMonkey.com
hi mahanandeesh belaganti
what heppen when any opportunity is deleted or undeleted ?? in this case your total_opportunity_amount__c field not update on account record
use below trigger code for do it
trigger sumofOpp on Opportunity (after delete, after insert, after undelete, after update) {
  
    if(trigger.isAfter){
        set<id> setOfAccId = new set<id>(); // create a set of contact account id 
       
         if(trigger.isInsert || trigger.isUnDelete){
			for(Opportunity oOpportunity :trigger.New){  
				 setOfAccId.add(oOpportunity.AccountId);   
 		   	  }  
          }

        if(trigger.isDelete){
			 for(Opportunity oOpportunity :trigger.old){
                setOfAccId.add(oOpportunity.AccountId);
			}
		 }  
        
         // create a list for update Account 
         List<Account> accountUpdate = new List<Account>();
         List<Account> getacc = [Select NoContact__c,total_opportunity_amount__c ,Id, (Select Id,Amount From opportunities) From Account WHERE ID IN :setOfAccId];
          for(Account acc :getacc){
             decimal Count = 0;
                for(opportunity oOpp: acc.opportunities){
                
                         acc.total_opportunity_amount__c += oOpp.Amount;
               
                } // inner for loop close
                
               
                 
                   
                    accountUpdate.add(acc);
                 
          }// outer for loop close
        
        if(accountUpdate.size() > 0){
          
          update accountUpdate ;
          
          }
       } // Trigger.isAfter closed
}
let me inform if it helps you
thanks


 
belabela
Hi Piyush,

Thanks for the reply,actually i want to know what is the mistake i did in my  trigger and why i am getting the error and how to solve it.so please help me to understand the mistake.