I want to prevent DML operations during certain processing-situations and provoke a Database.SaveResult with an Database.Error-List.
According to SObject class documentation, the method
addError() should "prevent any DML operation from occurring". As it turns out, this is not true. The following code commits the record to the database without any errors.
Account a = new Account(Name = 'Valid Corp Name Ltd.'); a.addError('Error one'); a.addError('Error two'); Database.SaveResult sr = Database.insert(a, false); System.debug(sr.isSuccess()); System.debug(sr.getErrors());
16:11:01:548 USER_DEBUG |DEBUG|true 16:11:01:548 USER_DEBUG |DEBUG|()
How can I prevent a record from beeing commited? I am aware of the similar question sObject.addError did not prevent dml operation, which did not get answered. So I am trying again.