function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Customize Exception message


I wanted to display the exceptions in a user friendly way on a visualforce page.

For example: the exception message i get is something similar to the one below

" xyzAfterInsert: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id xxxxxxxxxxxxx; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Close Date of the Opportunity cannot be in the past. Please update it accordingly.: [] Class.xxxxx_UpdateOpportunity.updatexxxOpportunity: line 159, column 1 Trigger.xyzAfterInsert: line 29, column 1"


instead i want to display

"The Close Date of the Opportunity cannot be in the past. Please update it accordingly"


how can i do that ?




This is what happens if you use the default DML options.


Instead, use Database.Update instead of update (or Insert, upsert, etc), check if there is a failed result, and if so, just return back to the page. Salesforce should put the message back on the screen as a normal field error.


Hi sfdcfox,

I have tried something like this in the code

Database.SaveResult sr=Database.insert(oppSupReq,false);       
            Database.Error err = sr.getErrors()[0];
            ApexPages.addMessages(new noMessageException(err.getMessage()));
            return null;


I don't see any change in the message displayed on the visualforce page.