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());
Debug log:
16:11:01:548 USER_DEBUG [5]|DEBUG|true
16:11:01:548 USER_DEBUG [6]|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.
addError
need to used against in Trigger operationsaddError
actually only works in trigger context, therefore I assumed the question was different.