5

I'm reading about transactions, how do async transactions fit in to that concept?

For example, say I have a sync method with a bunch of DMLs, and at the very end there's a DML then it's followed by a future method. If the sync method fails at the last DML, then everything rolls back. Ok, what happens if it succeeds, and goes to the async method, which fails? I'd have to think in this case, the operations exectured in the sync method wouldn't be able to be rolled back... But I'm not sure what would and wouldn't be able to be rolled back within the async method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm?search_text=transaction

4

The async method will trigger a new, separate transaction. So you're right, everything that's in the first transaction can be rolled back with a savepoint and once all that is complete it gets committed to the database, then the other transaction starts. At this point you would not have control over the first transaction so you would need to keep track of all your DML changes and revert back manually. Using @future, you will need to utilise collections of primitive types to pass information from the first transaction into the @future method. The queueable interface makes all this a little bit more easier as you can also pass sObjects and collections of sObjects as parameters into the queueable method.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.