Salesforce Decode
Salesforcedecode
Back to questions
ApexIntermediatesavepointtransactions

Use Savepoint and rollback for composite business operations

Real World Scenario

Opportunity creation with line items must rollback entirely if inventory check fails mid-process.

Expected Answer

• Savepoint sp = Database.setSavepoint() before risky section • Database.rollback(sp) on business rule failure • Savepoints dont rollback callout side effects—design accordingly • Limit savepoint nesting depth awareness • Clear user messaging on full rollback • Test rollback paths assert no partial records

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: Savepoint sp = Database.setSavepoint() before risky section Also consider: Database.rollback(sp) on business rule failure In practice: Savepoints dont rollback callout side effects—design accordingly Balance speed of delivery with maintainability.

Architect Perspective

Savepoints handle logical units of work—not compensation for external systems.