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
Nihar Annamaneni 7Nihar Annamaneni 7 

i am facing errors when deleting contact records...........

Error is :

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContactBeforeInsert1 caused an unexpected exception, contact your administrator: ContactBeforeInsert1: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ContactBeforeInsert1: line 2, column 1". 

My code :
trigger ContactBeforeInsert1 on Contact (before insert, before update, before delete) {
    for(Contact objContact : Trigger.new){
        if(trigger.isBefore){
            objContact.Description = 'Contact is created by ' + UserInfo.getUserName() + '. Description is added in before insert Trigger.';
         }
        if(trigger.isUpdate)
        {  
           objContact.Description = objContact.Description + 'Contact is updated by ' +UserInfo.getUserName() + '. Description is updated in before update Trigger.';
        }
       if(trigger.isDelete)
        {  
          // Create a task to track that contact is deleted
          Task objTask = new Task();       
          objTask.Subject = 'Contact Deleted';    
          objTask.Priority = 'Normal';        
          objTask.Status = 'Completed';                  
          objTask.Description = 'Contact ' + objContact.Id + ' is deleted by ' + UserInfo.getUserName() + '. Description on contact was ' + objContact.Description;
          try{
              insert objTask;
          }
          catch(Exception ex){
              
          }
        }
    }
}
 
Best Answer chosen by Nihar Annamaneni 7
SandhyaSandhya (Salesforce Developers) 
Hi,

In delete context Trigger.New is not available thats the reason its throwing error.
 
consider if you want to run a block of code based on delete then insert the logic as shown below
 
 if(Trigger.isDelete)
{
    if(Trigger.IsBefore || Trigger.IsAfter) // depends on criteria
    {
               // Your logic
    }
}
 
In this case the whole trigger wont run incase of deletion of record only specified block will run

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

In delete context Trigger.New is not available thats the reason its throwing error.
 
consider if you want to run a block of code based on delete then insert the logic as shown below
 
 if(Trigger.isDelete)
{
    if(Trigger.IsBefore || Trigger.IsAfter) // depends on criteria
    {
               // Your logic
    }
}
 
In this case the whole trigger wont run incase of deletion of record only specified block will run

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
This was selected as the best answer
SFDC RohitSFDC Rohit
Trigger.new is not available on before delete, and after delete triggers.  Use Trigger.old instead.  In case you're using the same trigger for insert/ update, which is the case I suppose, then try using Trigger.isInsert, Trigger.isUpdate, and Trigger.isDeletecontext variables to determine whether to use Trigger.old or Trigger.new.  Hope this makes sense.