+ Start a Discussion
woodmanzeewoodmanzee 

Setting Apex Error Message on Detail Page from Custom Button

I have a custom button that currently checks the value in a field on a custom object and either displays a javascript alert if the field isn't what we want or redirects to a new page if the field is valid. I'd rather do it within salesforce rather than with an alert box - is there a way to add an Apex Error message at the top of the page? Or a smiliar method?
Vinita_SFDCVinita_SFDC
Hello,

I am afraid if this is possible, you use validation though. Please refer following thred for validation:

http://salesforce.stackexchange.com/questions/8521/how-to-build-a-salesforce-custom-button-with-validation-rules
woodmanzeewoodmanzee
I actually found a way to do it, though it's not the cleanest way. The HTML element for save errors (the bold red text that shows up at the top, not the nice alert box like ApexPage messages have) always exists whether its populated and showing or not. So I'm just grabbing the element and populating it myself if an error occurs

var error = document.getElementById('errorDiv_ep');
if (error == null) {
alert('Error: Invalid Status. The Work Order Status must be set to New, Open, or Scheduled to Cancel it.');
} else {
error.innerHTML = 'Error: (ERROR HERE).<br> (ERROR MESSAGE HERE)';
error.style.display='block';
}