Question 1
IntermediateLogic and Process Automation · Error Handling and Transactional Integrity
A financial services company uses a custom Apex REST service to receive transaction data from a third-party system. The service must process batches of up to 200 transactions, create corresponding custom Transaction__c records, and update the related Account. If any transaction in the batch is invalid, the entire operation must be rolled back, and a specific error message must be returned. Which Apex feature is most appropriate for maintaining transactional integrity in this scenario?
Answer and explanation
Correct answer: C
Using Database.Savepoint and Database.rollback() provides explicit control over the transaction. By setting a savepoint before attempting the DML operations, the developer can catch any exception, roll back all changes to the state of the database before the savepoint was set, and then return a controlled error message. A simple try-catch with a thrown exception also rolls back the transaction, but using a savepoint is the canonical and most explicit way to manage partial transaction rollbacks or full rollbacks while maintaining control over the execution flow to return a specific response.
