Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How can I solve the issue of “you have uncommitted work pending. Please commit or rollback before calling out”?

Asked by Dylan Forsyth Feb 5, 2024 3.8K views 2 answers
Share

About this question

When I was going through my particular task in a Salesforce development environment then I recently received an error message that was showing “You have uncommitted work pending. Please commit or rollback before calling out”. This error message came when j was attempting to make a fallout after performing DML operations. Now, how can I solve this particular issue? 

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Apr 2, 2025

This error usually occurs in Salesforce Apex when you try to make an HTTP callout while there is an uncommitted database transaction pending. Salesforce doesn’t allow callouts if there are uncommitted changes in the database to maintain data consistency.

Why Does This Happen?

  • You are trying to perform a callout (HTTP request to an external system) inside a transaction that has uncommitted database operations (DML operations like insert, update, or delete).
  • Salesforce enforces this restriction to prevent data inconsistencies if the callout fails.

How to Fix It?

 1. Move the Callout Before DML Operations

Ensure that the HTTP callout happens before any database changes.

HttpResponse response = makeCallout(); // Perform callout first
if(response.getStatusCode() == 200) {
    updateRecord(); // Perform DML after callout
}

 2. Use @Future Method

Callouts inside an @future method run asynchronously, avoiding this error.

@future(callout=true)
public static void makeCalloutFuture() {
    HttpResponse response = makeCallout();
}

Note: @future methods can’t return values or be chained.

 3. Use Queueable Apex for More Flexibility

If you need to handle more complex scenarios, use Queueable Apex instead of @future.

public class CalloutJob implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        HttpResponse response = makeCallout();
    }
}

This approach allows chaining jobs and handling results better.

Final Thoughts

  • Always perform callouts before DML operations when possible.
  • Use @future or Queueable Apex if you must make a callout after DML.
  • Avoid mixing DML and callouts in the same synchronous transaction.


Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.