[SalesForce] Displaying error message from Apex Code

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

Best Answer

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