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
Rung41Rung41 

Custom Error not displaying on page

 

I have created a trigger to prevent users from deleting an Account if a field is populated. However, instead of the error message appearing on the Account page I get this:

 

 

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 "This Account has a Community Code therefore it can not be deleted". 

Clickto return to the previous page.

 

 

Do I need a  "catch"?

 

My trigger:

  trigger CommunityCode on Account  (before delete)
  {
    if(System.Trigger.IsDelete)
        {
for (Account a : Trigger.old) 
if (a.Community_Code__c != Null) {
a.addError ('This Account has a Community Code therefore it can not be deleted');
}
}
}

 

SrikanthKuruvaSrikanthKuruva

replace the line

a.addError ('This Account has a Community Code therefore it can not be deleted');

 with

trigger.new[0].addError ('This Account has a Community Code therefore it can not be deleted');

 and let me know if this works

Rung41Rung41

No dice. I got

 

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 CommunityCode caused an unexpected exception, contact your administrator: CommunityCode: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CommunityCode: line 7, column 1". 

Click here to return to the previous page.



dfpittdfpitt

Hello,

 

Did you find any solution to this problem?

 

How can we catch delete errors on triggers in a VF page?