You need to sign in to do that
Don't have an account?

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){
}
}
}
}
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){
}
}
}
}
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
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