I'm working on implementing a robust error logging framework for our org using Platform Events to ensure that we always capture errors in our code even if the transaction is rolled back.
If I use Database.insert(listRecords,false)
, I parse the Save/Upsert/etc Result List for errors and then log those errors. In scenarios where I am explicitly using allOrNothing = false
, I expect partial DML and for the transaction to be at least partially committed to the database, so this works great in situations like this.
However, when it comes to try{}catch(DMLException){}
exception handling, what I am looking for is the ability to still capture/handle the DML Exception, but have Salesforce perform a full
transaction rollback the same way that it would if it had encountered an unhandled exception.
From my understanding of rollback
, it only works if I first create a savePoint
, but if I only create my savePoint
before the DML that I am handling exceptions for, then everything that occurs in the transaction prior to that savePoint
would still be committed to the database (for example, before insert
/before update
field changes made before the failing DML).
Question: Is it possible for us to both handle the exception for logging purposes AND rollback the entire transaction?