does someone know how to show an error in an object's listview after a dml operation?

I have written a simple trigger that prevents user to delete closed opportunity

List<Opportunity> opportunities = [select Id, isClosed from Opportunity where id IN: Trigger.oldMap.keySet()];

for (Opportunity opp : opportunities)
    if((opp.IsClosed ) opp.addError('Closed opportunities cannot be deleted');

It works, but obviously the error message is displayed just in the record detail page. If I try to delete a record from the list view (that it's right what I need) I get: 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......"

Is there a way to solve this issue without writing a custom visualforce page? Thank you

  • 1
    There's an <apex:messages> and an <apex:pageMessages>. Each has slightly different behavior. Using Layout Page from the App Exchange, you should be able to convert the original Standard Page Layout to VF to see which it uses and try using the other. See the Apex Code Developer's Guide to understand the difference between the two along with the flags available. – crmprogdev Nov 21 '14 at 16:13
  • when I've had this issue, and assuming, the deletion is for a single record, I overrode the standard Delete button with a VF page that does the delete action and hence can display the pageMessage – cropredy Nov 21 '14 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.