The error “Uncommitted work pending” is a typical Salesforce exception that is raised when a transaction tries to make a callout after it has already performed a database operation. It is most commonly encountered during the execution of Apex and can cause interruptions in your integrations, automation, and users’ actions.
What Is the Uncommitted Work Pending Error?
The error message typically looks like this,
System Callout Exception: You have uncommitted work pending. Please commit or rollback before calling out.
Transaction control is strict in Salesforce. Once you perform a DML operation (insert, update, or delete), Salesforce does not allow HTTP callouts in the same transaction, unless the callout happens first.
Uncommitted Work Pending Error Common Causes
1. Performing DML Before an HTTP Callout
If an Apex method executes a database operation before making an external API call, Salesforce blocks the callout.
Example scenario:
- Insert or update a record
- Then attempt a REST or SOAP callout
2. Callouts Inside Triggers
3. Mixed Automation (Flows, Process Builder, Apex)
4. Exceptions Before Transaction Completion
How to Fix Uncommitted Work Pending Error?
Solution 1. Perform Callouts Before DML Operations
Make HTTP callouts first in your Apex code before any insert, update, or delete statements.
Best practice:
- Make the callout
- Process the response
- Perform DML operations last
Solution 2. Use @future(callout=true)
Move the callout logic into an asynchronous Apex method. This allows Salesforce to complete the transaction first and then execute the callout separately.
Use case:
- Triggers
- After insert or update actions
- Integration scenarios
Solution 3. Use Queueable Apex with Callouts
Queueable Apex supports callouts and provides better control and chaining compared to future methods.
Why use Queueable?
- Supports complex logic
- Easier debugging
- Handles larger payloads
Solution 4. Avoid Callouts Directly from Triggers
Triggers should only collect data and pass it to helper classes or async processes.
Recommended approach:
- Trigger → Helper Class → Queueable / Future Method
Solution 5. Separate DML and Callouts into Different Transactions
- One transaction handles database changes
- Another transaction handles external integrations
Best Practices to Prevent This Error
- Never mix DML operations and callouts in the same synchronous execution
- Use async Apex for integrations
- Keep triggers lightweight
- Centralize callout logic in service classes
- Test with automation (Flows + Apex) enabled
When Does This Error Commonly Appear?
- Integrating Salesforce with third-party APIs
- Running Apex triggers with callouts
- Using Salesforce CPQ or custom pricing engines
- Executing complex Flows combined with Apex classes
Final Thoughts
The Uncommitted Work Pending error is not a bug, but rather a transaction rule in Salesforce that ensures the integrity of your data. Restructuring your code to separate DML operations from callouts is how you can fix and avoid this problem. If you’re facing this error repeatedly, reviewing your automation flow and integration design is the best long-term fix.
