+ Start a Discussion
Richie DRichie D 

Stopping DMLExceptions from reaching VF pagemessages



We're trying to stop DMLExceptions from reaching the user in Visualforce pages which come from a controller. try{} catch{} blocks aren't stopping the message being shown. 



Account a = [select id from Account limit 1];
				insert a; //fails due to supplied id
			}catch(DMLException ex){}

 From this we get the message:


Account ID: cannot specify Id in an insert call - Standard SFDC Exception Message


Is there anyway to stop the message being shown? The idea is to build our own message which is more friendly to the user.


Looking at the docs shows examples where a custom message is being shown to the user within a try/catch block with no mention of duplicate messages.


Any ideas?


Thanks for any help.



Use the Database.dml functions (e.g. Database.insert), supplying the second parameter as "false". Make sure you capture the return value from this function (see docs). If isSuccess() returns false, you can display your own error message instead.


Thanks sfdcfox. 


That works up to a point, but  now we are having the following problem. The page allows the user to update multiple records at once. When we display the error we want to tell the customer which record is causing the problem. You can interrogate a DML Exception with 




but there seems to be no way of getting the first failed ID out of a database result. getID() only returns an ID if it is successful!


Any ideas?


Thanks again


Naomi (Richie D's collegue)