What happens when an Apex method performs several database operations, but one of them fails halfway through?
For example, imagine a Salesforce process that creates an Account, adds related Contacts, and then updates an Opportunity. If the final operation fails, you may not want the earlier changes to remain saved.
This is where Apex Savepoints and Rollbacks become useful.
They allow developers to control database changes within a transaction and restore the database to a previously established point when necessary.
Let's understand how they work with a practical example.
- What Is a Savepoint in Apex?
A Savepoint represents a point within the current Apex transaction to which database changes can be rolled back.
Salesforce provides two methods:
Database.setSavepoint() – Creates a savepoint.
Database.rollback() – Reverses database changes made after that savepoint.
This is useful when multiple operations must succeed together.
- Basic Apex Example
Consider a method that creates an Account and a related Contact.
public class CustomerService {
public static void createCustomer() { Savepoint sp = Database.setSavepoint(); try { Account acc = new Account( Name = 'Tech Solutions' ); insert acc; Contact con = new Contact( LastName = 'Sharma', AccountId = acc.Id ); insert con; } catch (Exception e) { Database.rollback(sp); System.debug( 'Transaction failed: ' + e.getMessage() ); } } }
If the Contact insertion fails, Salesforce rolls back the Account insertion made after the savepoint.
This helps prevent partially completed database operations.
- Understand How Database.rollback() Works
Rollback reverses database changes made after the selected savepoint within the current transaction.
For example:
Create a savepoint.
Insert an Account.
Insert a Contact.
An error occurs.
Roll back to the savepoint.
The Account and Contact changes made after the savepoint are reversed.
One important detail: rollback does not undo every possible side effect. External API calls, emails already sent, and other actions outside the database transaction may not be reversed.
- Use Savepoints With Database Operations
Savepoints can also be useful when working with Salesforce Database methods.
For example, developers may use Database.insert(records, false) to allow partial success across records.
However, partial-success operations and full transaction rollbacks are different approaches.
If your business process requires all database changes to succeed together, you can use a savepoint and explicitly roll back when a failure condition occurs.
Choose the approach according to the required business behavior.
- Important Limitations to Remember
Before using savepoints in production Apex, remember:
Savepoints work within the current transaction.
Salesforce limits the number of savepoints in a transaction.
Rolling back does not reverse external system actions.
Savepoints do not create independent transactions.
DML and other transaction limits still apply.
Savepoints can be invalidated by certain transaction-control operations.
Use savepoints only when the business process needs rollback behavior.
- Best Practices for Apex Developers
Create a savepoint before the database operations you may need to reverse.
Use clear exception handling.
Avoid unnecessary savepoints.
Keep transaction logic manageable.
Do not assume external API actions can be rolled back.
Test both successful and failed execution paths.
Final Thoughts
Apex Savepoints and Rollbacks help developers manage database changes when a transaction fails.
They are especially useful when several related DML operations must succeed as one business process.
Understanding transaction behavior helps developers build more reliable Salesforce applications and avoid incomplete database updates.
For more Salesforce development and implementation guidance, explore iBirds Software Services.
Discussion: Have you used Apex Savepoints in a real Salesforce project? What transaction scenario did you use them for?

