System.CalloutException: You have uncommitted work pending: meaning and how to fix it

Apex runtime Checked against the official docs on Source: SOAP API Developer Guide: Core Data Types, StatusCode

Quick answer

System.CalloutException: You have uncommitted work pending: Apex made a callout after DML in the same transaction. A callout is not allowed while there are uncommitted changes. Start with: Make the callout before the DML.

Typical message: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Cause

Apex made a callout after DML in the same transaction. A callout is not allowed while there are uncommitted changes.

How to fix System.CalloutException: You have uncommitted work pending

  1. Make the callout before the DML.
  2. Or move the callout (or the DML) to a Queueable or @future method so it runs in its own transaction.

Have a different message? Paste it into the error decoder, which matches it against 45 common Salesforce errors in your browser.

Frequently asked questions

What does System.CalloutException: You have uncommitted work pending mean in Salesforce?

Apex made a callout after DML in the same transaction. A callout is not allowed while there are uncommitted changes.

How do I fix System.CalloutException: You have uncommitted work pending?

1. Make the callout before the DML. 2. Or move the callout (or the DML) to a Queueable or @future method so it runs in its own transaction.

More Apex runtime errors

SFDX Talk is unofficial documentation. It is not affiliated with, endorsed by, or sponsored by Salesforce, Inc. Message text varies slightly by release and context, so confirm against the official page before relying on it.