All articles

Platform

Uncommitted work pending: fix Apex callout ordering

Choose callout-first or a separate asynchronous transaction, and handle savepoints without assuming releaseSavepoint commits pending DML.

A callout after a Salesforce write can fail before the remote service receives anything. Inspect the transaction’s earlier operations before debugging the network.

Salesforce’s HTTP guidance documents callout restrictions when pending operations exist. Check helpers, triggers and enqueued jobs for pending work as well as the DML statements beside the callout.

Choose the order from the business requirement

If the remote response is needed to calculate the Salesforce update, make the callout before the write where the transaction design permits it. Validate inputs before the callout, then apply the confirmed result.

That order still needs recovery. The external action may succeed while the later Salesforce update fails. Use a stable operation identifier and a reconciliation path so a retry doesn’t perform the remote action twice.

If the Salesforce record must commit first, use a separate asynchronous transaction for the callout. A Queueable implementation making callouts uses Database.AllowsCallouts; it must still obey ordering rules inside its own transaction. Writing a status record immediately before the worker’s callout can recreate the same problem.

Make asynchronous status visible

Record that work is requested, pending, confirmed or failed according to the actual integration contract. Don’t mark the original record “synced” merely because a job was enqueued.

Provide an operator with the job or operation identifier, the last confirmed stage and a retry decision. A timeout may leave the remote result unknown. In that case, reconcile before retrying a consequential operation.

For a Salesforce-to-ServiceNow integration, choose which system owns the ticket state and how each side recognizes a repeated update. The ServiceNow implementation service starts with that ownership and recovery design.

Treat savepoints as rollback controls

Transaction-control documentation explains the supported sequence for rolling back uncommitted DML and releasing savepoints before a callout. Releasing a savepoint alone does not undo pending writes and is not a mid-transaction commit.

Don’t add a savepoint workaround without deciding whether the earlier changes should be discarded. Review the class API version and every active savepoint. A syntactically valid rollback can still violate the intended business process.

Test both sides of the boundary

Use callout mocks for success, rejection and uncertain-response handling. Test a remote success followed by a Salesforce validation failure, and a retry of the same logical request. Assert the record state and external-operation identifier alongside the mock invocation.

The exception is a transaction-design signal. Fixing the order should leave a clear account of what happened when only one side completed.

Keep reading

All articles