How to display error message at the top of page in Salesforce from Apex? (I'm not using Visualforce)


You can display error messages using the addError method on sobject or its fields.

Salesforce docs on the sobject instance addError method: https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError

More info on exception handling: http://wiki.developerforce.com/page/An_Introduction_to_Exception_Handling

  • Thank you for your answer. addError works, however it appends "Error: Invalid Data. Review all error messages below to correct your data." before the error message. Any ideas how to disable that? – justasd Feb 10 '14 at 15:58
  • 2
    Other ways you could go about it are to call addError on a specific field like Account.Name.addError(''). This would give you a different, but smaller prefix to the error. Additionally, you could write a validation rule on the sobject to show an error instead of handling it in Apex. This would show a Validation error instead of Invalid data. If none of those suffice, the only other route is more customization through either Visualforce or Javascript. – James Loghry Feb 10 '14 at 16:46
  • I think using Visualforce would be too much for this scenario. Then I would need to add a page and a controller. I am searching the web how to call JavaScript from Apex class but couldn't find the solution. Is it even possible? (Regular Apex class, not controller) – justasd Feb 10 '14 at 17:50
  • Fixed link in case someone else will try to click the first link in the answer above: developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/… – MenashYA Apr 17 '17 at 19:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.