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
CloudPowerCloudPower 

Error while using adderror in a trigger while delete operation:

Hi,

When i handle the validation from trigger using trigger.new.adderror('Message...') for delete functionality the error is being displayed but not in the same page it is being displayed in the system page(new page) as a runtime exception.

 

The error displayed is:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "MSG_CANNOT_DELETE_RECORD_WITH_ASSOCIATION : Record cannot be deleted because it is associated with another record.". 



 

 

Have even used return; keyword after adding the adderror functionality inorder to avoid the other operations to be handled but even then this message is being raised.

Please let me know if anyone has a soln inorder to avoid the issue.

 

Thanks

Rahul SharmaRahul Sharma

I think in your case you are not checking for an error, and the page is redirected to new page.

So for that, Put the delete statement in try-catch and in catch show appropriate page message.

Also note to return null when there is exception in delete.

 

CloudPowerCloudPower

Thanks for the reply.

Checked in other scenarios also this seems to be a new behaviour of salesforce so as to display the custom validation error if handled in the trigger in delete case.where as its working as it use to do while update.

Rahul SharmaRahul Sharma

I think in your case what happening is, you are deleting the record and directly redirecting to a page.

Due to which what happens is the validation occurs in next page.

Thats not the new behaviour of salesforce. I too had faced that issue.

CloudPowerCloudPower

I am doing just a delete operation and checking a condition if it is satisfies the criteria then will throw a validation using addError.

ex:

if(trigger.isdelete)

{

if(condition1....)

trigger.new.addError('Message...');

}

 

Please let me know if this code is cause for the issue.

Rahul SharmaRahul Sharma

I believe you are getting that error in custom vf page, if yes then post code snippet

CloudPowerCloudPower

No I am handling the issue in the standard page.

Rahul SharmaRahul Sharma

you should use code as :

trigger.new[0].addError('Message...');

 

CloudPowerCloudPower

Yes Tried the using the same code but still the same issue.

 

Thanks

Amol SableAmol Sable

@ Rahul it is not working

Rahul SharmaRahul Sharma
Sorry my bad, Trigger.addError in before delete trigger always shows the error message in a new page.
For avoiding this I had overridden delete button with a visualforce page to show user friendly message as a page Message.
balagani1.3914993504421423E12balagani1.3914993504421423E12
Hi Rahul Sharma,

Trigger.addError in before delete trigger always shows the error message in a new page.
Instead of showing the error message in new page, I need to show in same standard page.
Is it possible...!

And I have the condition to not to delete the record when the active field is checked.

Thanks in Advance.

Rakesh