Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 4 down vote accepted

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

Salesforce docs on the sobject instance addError method:

More info on exception handling:

share|improve this answer
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
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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