You need to sign in to do that
Don't have an account?
CloudPower
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
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.
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.
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.
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.
I believe you are getting that error in custom vf page, if yes then post code snippet
No I am handling the issue in the standard page.
you should use code as :
Yes Tried the using the same code but still the same issue.
Thanks
@ Rahul it is not working
For avoiding this I had overridden delete button with a visualforce page to show user friendly message as a page Message.
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