Transaction Control
Learning Content
Last updated: July 21, 2026
Transaction Control in Apex
Apex provides built-in Transaction Control to ensure that database operations are executed reliably and consistently. A transaction is a group of operations that Salesforce treats as a single unit of work. If every operation succeeds, Salesforce commits the transaction. If an unexpected error occurs, Salesforce rolls back the transaction to maintain data integrity.
This feature helps prevent incomplete or inconsistent data from being stored in the database, ensuring that business processes remain accurate and reliable.
💡 LearnFrenzy Insight
Imagine transferring ₹10,000 from one bank account to another.
The banking system must complete two operations:
- Debit ₹10,000 from Account A
- Credit ₹10,000 to Account B
If the first operation succeeds but the second fails, the money cannot simply disappear. The bank cancels the entire transaction to keep both accounts accurate. Salesforce Apex follows the same principle.
Why Do We Need Transaction Control?
Business operations often involve multiple database actions that depend on one another. If only some of those actions succeed, the database can become inconsistent.
Transaction Control ensures that either all operations succeed together or none of them are permanently saved.
| Business Process | Why Transaction Control? |
|---|---|
| Create Account and Contact | Both records should be saved together. |
| Order Processing | Order, Invoice, and Inventory must stay synchronized. |
| Payment Processing | Prevent partial financial updates. |
| Case Resolution | Update multiple related records consistently. |
| Bulk Data Import | Maintain data integrity during processing. |
How Transaction Control Works
Start Transaction
│
▼
Execute Apex Logic
│
▼
Perform Database Operations
│
▼
───────────────
│ Success? │
───────────────
│ │
Yes No
│ │
▼ ▼
Commit Rollback
Salesforce automatically determines whether the transaction should be committed or rolled back based on the outcome of the execution.
Simple Transaction Example
The following example inserts a new Account record. If the operation completes successfully, the transaction is committed automatically.
// Execute Anonymous Window
Account acc = new Account(
Name = 'LearnFrenzy Technologies'
);
insert acc;
System.debug(
'Account Created Successfully'
);
Debug Log Output
USER_DEBUG [6]|DEBUG|Account Created Successfully
If an unexpected error occurs before the transaction completes, Salesforce automatically rolls back the changes to maintain data consistency.
Real Business Scenario
🏢 Business Example
Suppose a customer places an order through Salesforce.
The application performs several operations:
- Create an Order record.
- Create an Invoice.
- Update Inventory.
- Create a Shipment Request.
If updating the inventory fails, Salesforce can roll back the entire transaction so that incomplete order information is not stored.
Key Transaction Concepts
| Concept | Description |
|---|---|
| Transaction | A group of operations treated as one unit. |
| Commit | Permanently saves all successful changes. |
| Rollback | Reverts changes when an error occurs. |
| Savepoint | Marks a point that can be restored later. |
| Exception | An error that may cause the transaction to fail. |
✅ Best Practice
Design your Apex code so that related business operations execute as a single logical transaction. This helps maintain data consistency and simplifies error handling.
⚠️ Common Mistake
Many beginners assume that every database operation is saved immediately. In reality, Salesforce treats all operations within a transaction as a single unit and may roll back the changes if an unhandled error occurs before the transaction completes.
🎯 Interview Tip
Question: What is a transaction in Apex?
Answer: A transaction is a group of database operations that Salesforce executes as a single unit of work. If all operations succeed, the transaction is committed. If an unhandled error occurs, Salesforce rolls back the transaction to preserve data integrity.
📚 Learn More
This section introduces Transaction Control as one of Apex's core platform features.
In the dedicated Transactions & Savepoints chapter, you'll learn:
- Commit and Rollback
- Savepoints
- Partial Success with Database Methods
- Transaction Boundaries
- Error Recovery Strategies
- Real Business Scenarios
- 30+ Practical Examples
Practice What You've Learned
Test your understanding with these practice exercises