Sample application to showcase how error handling can be done in a Salesforce app.
We wanted to compare the outcome when DML was performed in these three different ways:
- Simple DML not contained in a try-catch
- Simple DML wrapped in a try-cach
- DML Performed through the Database class
The purpose of the following tests is to allow us to inspect the Database.SaveResult records and see other relevant information through the Debug statements in the error log:
All these tests use the Helper class, where the two most interesting pieces are the ones that associate an error with the record that caused it. This happens in the following places:
- In the catch blocks if we made a simple Dml (not using the Database class)
- In the processDml method is we performed the Dml through the Database class
The tests that verifies those pieces of code are working properly are in the AddError_TEST class.
Visualforce Page Purpose
The VisualForce page (Test1.page) allows us to see how the error message that we manually put inside the SObject using addError is displayed by default in the apex:pageMessages element. Also, we were able to test that no error message is shown in this element if we use the Database dml methods but don't manually add the error message to the object.
The DML methods of the Database class have very interesting behavior. They will roll back for certain types of errors, and then run again with only those records that did not fail in the first try.
For example of how Database.insert behaves look at the tests in the ERR_Handler_TEST class.