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
mohan s 37mohan s 37 

I had faced following error in my trigger when i try to perform delete operation but it works fine for insert operation

Hi Friends,
I have custom field called NoContact__c It is a decimal data type in Account object,. My requirement is to increment the NoContact__c field each time when i try to insert the contact for associated account.And when i delete the contact that associate with Account, The NoContact__c  filed should be decremented. For this requirement i had written trigger in this trigger inserting operation is working fine but when i try to perform delete operation it throws the following error. Can any one help me what mistake did i made in this trigger.

Error: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point".

Apex trigger:
    trigger countcontacts on Contact (after insert,before delete) {
      List<Contact>conlist=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id IN:trigger.new];
         List<Account> toupdateaccounts=new List<Account>();
             List<Account> updatedlist=new List<Account>();
          for(Contact c: conlist){
     c.Account.NoContact__c=c.Account.NoContact__c+1;
    updatedlist.add(c.Account);
 }
 update updatedlist;
    if(trigger.isDelete){
      List<Contact> contactstodelete=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id      IN:trigger.old];
      for(Contact deletedcontact:contactstodelete){
       deletedcontact.Account.NoContact__c=deletedcontact.Account.NoContact__c-1;
           toupdateaccounts.add(deletedcontact.Account); 
        }
       update toupdateaccounts;
    }
}
Best Answer chosen by mohan s 37
sfdcMonkey.comsfdcMonkey.com
hi mohan
what happen if any contact is Undelete ?? in this case your NoContact__c field on account is not update and above trigger is not good approach for achieve this
use below trigger
trigger countcontacts on Contact (after delete, after insert, after undelete) {
  
    if(trigger.isAfter){
        set<id> setOfAccId = new set<id>(); // create a set of contact account id 
       
         if(trigger.isInsert || trigger.isUnDelete){
			for(contact ocontact :trigger.New){  
				 setOfAccId.add(ocontact.AccountId);   
 		   	  }  
          }

        if(trigger.isDelete){
			 for(contact ocontact :trigger.old){
                setOfAccId.add(ocontact.AccountId);
			}
		 }  
        
         // create a list for update Account 
         List<Account> accountUpdate = new List<Account>();
         List<Account> getacc = [Select NoContact__c, Id, (Select Id From contacts) From Account WHERE ID IN :setOfAccId];
          for(Account acc :getacc){
             Integer Count = 0;
                for(contact ocon: acc.contacts){
                
                          Count++ ;
               
                } // inner for loop close
                
                if(acc.NoContact__c != Count){
                 
                    acc.NoContact__c = Count ;
                    accountUpdate.add(acc);
                 }
          }// outer for loop close
        
        if(accountUpdate.size() > 0){
          
          update accountUpdate ;
          
          }
       } // Trigger.isAfter closed
}

Please mark it best answer if it helps you so it make proper solution for others in future 
and let me inform if it helps you
Thanks

 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi mohan
what happen if any contact is Undelete ?? in this case your NoContact__c field on account is not update and above trigger is not good approach for achieve this
use below trigger
trigger countcontacts on Contact (after delete, after insert, after undelete) {
  
    if(trigger.isAfter){
        set<id> setOfAccId = new set<id>(); // create a set of contact account id 
       
         if(trigger.isInsert || trigger.isUnDelete){
			for(contact ocontact :trigger.New){  
				 setOfAccId.add(ocontact.AccountId);   
 		   	  }  
          }

        if(trigger.isDelete){
			 for(contact ocontact :trigger.old){
                setOfAccId.add(ocontact.AccountId);
			}
		 }  
        
         // create a list for update Account 
         List<Account> accountUpdate = new List<Account>();
         List<Account> getacc = [Select NoContact__c, Id, (Select Id From contacts) From Account WHERE ID IN :setOfAccId];
          for(Account acc :getacc){
             Integer Count = 0;
                for(contact ocon: acc.contacts){
                
                          Count++ ;
               
                } // inner for loop close
                
                if(acc.NoContact__c != Count){
                 
                    acc.NoContact__c = Count ;
                    accountUpdate.add(acc);
                 }
          }// outer for loop close
        
        if(accountUpdate.size() > 0){
          
          update accountUpdate ;
          
          }
       } // Trigger.isAfter closed
}

Please mark it best answer if it helps you so it make proper solution for others in future 
and let me inform if it helps you
Thanks

 
This was selected as the best answer
mohan s 37mohan s 37
Thank you piyush I got exact answer what i am loking for. It would help me a lot.