1

According to salesforce docs on savepoints and rollbacks:

Apex gives you the ability to generate a savepoint , that is, a point in the request that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, and the database can be restored to the same condition it was in at the time you generated the savepoint.

When it says any DML statement that occurs after the savepoint, does it mean within that transaction, or across all changes that might have happened?

Also, is the manual savepoint the same as the automatic rollback on exception?

I have a future method making a callout that is then updating records - at the moment the entire method is wrapped in a 'try catch' which I assume will stop rollbacks from happening (the catch currently sends an email in the event of failure and records are manually updated).

If I were to get rid of the email and remove the try catch, or rethrow an exception, or add a savepoint and revert manually, is there a danger that this might revert changes made in the meantime by other transactions?

@future(callout=true)
public static void retrieveAndProcessContactData(String contactCode) {
    try {  
        contact retrievedCon = getContact(contactCode);

        if (retrievedCon != null) { 
            createOrUpdateContact(retrievedCon);
        }
    } catch(Exception ex) {
        processingExceptionEmail(ex, 'contact processing failure', contactCode);
        System.debug('ERROR:');
        System.debug(ex);
    }
}

1 Answer 1

4

When it says any DML statement that occurs after the savepoint, does it mean within that transaction, or across all changes that might have happened?

Only within the same transaction.

Also, is the manual savepoint the same as the automatic rollback on exception?

Generally speaking, yes. Everything that would be rolled back by exception is also rolled back, including insert, update, upsert, undelete, delete, sendEmail, future, enqueueBatch, System.schedule, enqueueJob, etc. The only difference is that it only works up to the Database.setSavePoint call (meaning, any DML before that point would still commit). Stated differently, an unhandled exception basically triggers a SavePoint that was generated at the very beginning of the transaction.

If I were to get rid of the email and remove the try catch, or rethrow an exception, or add a savepoint and revert manually, is there a danger that this might revert changes made in the meantime by other transactions?

No, no other transactions would be affected, only the work in the future method is lost. This would be true for automatic exception handling or manual rollbacks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .