Print this page

You have uncommitted work pending. Please commit or rollback before calling out"

Knowledge Article Number 000003701

Sometimes a developer needs to create a record and then update it with information provided by a Web Service. However, a Web Service Callout may not occur after a DML statement within the same transaction. To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs.


This workaround splits the transaction into two separate Ajax processes. The first inserts the record and the second performs the callout and is able to update the newly inserted record.

<!-- -->

<apex:page controller="TestWsCallout" tabstyle="Account">
    <apex:form >
        <apex:actionFunction action="{!InsertRecord}" name="InsertRecord_JS" Rerender="statuses" status="Status1" oncomplete="CallWebService_JS();"/>
        <apex:actionFunction action="{!CallWebService}" name="CallWebService_JS" status="Status2" reRender="statuses, msg"/>
        <apex:outputPanel id="statuses">
            <apex:actionStatus id="Status1" startText="...Inserting Record Into DB..." />
            <apex:actionStatus id="Status2" startText="...Calling Web Service..." />
        <apex:outputPanel id="msg">
            <apex:pageMessages />
        <div><input name="DoAction" class="btn" type="button" value="Do Action" onclick="InsertRecord_JS();return false;"/></div>


// TestWsCallout.cls

public class TestWsCallout{
    Account myAccount;
    public PageReference InsertRecord() {
        myAccount = new Account(name = 'Test Account');
        insert myAccount;
        // Calling a Web Service here would throw an exception
        return null;
    public PageReference CallWebService() {
        // Execute a call to a Web Service
        HttpRequest req = new HttpRequest();
        req.setEndpoint('' + myAccount.Id);
        HttpResponse response = new Http().send(req);
        // Simulate an update
        myAccount.Name = 'Test Account 2';
        update myAccount;        
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'WebService Called on New Account: ' + myAccount.Name));
        return null;

promote demote