Take the 2-minute tour ×
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.

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

share|improve this question

1 Answer 1

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: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_SObject_addError.htm

More info on exception handling: http://wiki.developerforce.com/page/An_Introduction_to_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 at 15:58
1  
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 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 at 17:50

Your Answer

 
discard

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.