The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Using Apex savepoints to roll back a failed multi-step transaction (opens in a new tab)

You will learn how Database.setSavepoint() and Database.rollback() undo DML made after a savepoint, and where that protection stops, such as external callouts and emails.

Why read this

The article includes a short worked example.

  1. Call Database.setSavepoint()
  2. Insert the Account
  3. Insert the related Contact
  4. Contact insert fails: catch the exception
  5. Call Database.rollback(sp) to undo the Account
How a savepoint lets a failed Apex transaction undo its earlier database changes

The article describes a common problem: an Apex method performs several database operations, and a later one fails, leaving earlier changes saved. Savepoints address this by marking a point in the current transaction that can later be restored.

It presents two methods. Database.setSavepoint() creates the marker, and Database.rollback() reverses database changes made after it. The worked example creates an Account and a related Contact inside a try block, and on exception rolls back to the savepoint so the Account insert is undone.

The author notes that rollback does not undo every side effect. External API calls and emails already sent are outside the database transaction and are not reversed. The article also contrasts savepoints with partial-success DML using Database.insert(records, false), which is a different approach from a full rollback.

The limitations listed include that savepoints apply only within the current transaction, that Salesforce caps the number of savepoints, that certain transaction-control operations can invalidate them, and that DML and other transaction limits still apply. Best practices suggest creating savepoints only before operations that may need reversing, using clear exception handling, and testing both success and failure paths.

Read the original at dev.to (opens in a new tab)

Worth reading?

Seven Apex best practices for handling governor limits and bulk processing (opens in a new tab)

Readers will learn how to structure Apex transactions to avoid governor limit exceptions by applying bulkification patterns, optimizing data operations, and designing realistic test suites.

The article explains that Salesforce multitenancy requires strict resource management through governor limits that cap SOQL queries, DML statements, CPU time, and heap size. Developers are advised to treat these boundaries as foundational design constraints rather than post-development hurdles.

Core recommendations focus on eliminating database operations from iterative loops. The piece demonstrates collecting target identifiers into sets, executing single bulk queries, and populating maps for rapid lookups. It also shows how to accumulate records into lists for batch insertions while checking for empty collections beforehand.

Efficient use of Apex collections and comprehensive testing strategies receive equal emphasis. Writers are instructed to select appropriate collection types based on lookup needs and to validate logic against high record volumes. Testing must account for downstream automation overhead and varying data relationships to prevent unexpected limit exhaustion.

Finally, the guide outlines ongoing maintenance practices, including reviewing debug logs and refactoring inefficient code as data scales. It notes that persistent limit failures often stem from interconnected automation layers, requiring architects to evaluate broader system design alongside individual class performance.

Read the original at dev.to (opens in a new tab)

Worth reading?