0 votes
2 views
by

In the code below:

try {
   //code here
}
catch (Exception e) {
   Account a = new Account;
   insert a;
   Throw new Exception ('test')
}

Apparently on Platform Event objects, executing the command:

Database.SaveResult sr = EventBus.publish (logErro);

The insert is done. But what about normal objects? How can I make my insert effective even after throwing a throw exception after that?

Thanks.

1 Answer

0 votes
by
 
Best answer

You can't. All normal sObject DML is rolled back if an unhandled exception is thrown.

Platform Events will escape only if they're configured to be non-transactional (not configured for "Publish After Commit"). If you need to persist data, you could include it as the payload of a non-transactional Platform Event that is persisted by a Platform Event trigger elsewhere in your application.

This pattern is rarely suitable for anything except error logging. It's almost certainly not suitable for inserting an Account record.

That's not a bug - it's how the platform is supposed to work. An unhandled exception means that something bad has happened and the database may now be in an inconsistent state. The platform protects you and your users by rolling the transaction back to a previous consistent state.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...