Callouts are not allowed when there is an uncommitted transaction pending. For example, if a savepoint is set before a Data Manipulation Language (DML) operation, a callout cannot be made based on the results of that operation to commit or roll back the savepoint.
Sometimes during the processing of records, your business rules require that partial work (already executed DML statements) is rolled back so that the processing can continue in another direction. Apex gives you the ability to generate a savepoint, that is, a point in the request that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, restoring the database to the condition it was in when you generated the savepoint.
Example scenario:
When trying to perform a savepoint, insert an account to find duplicates and rollback and then perform a webservice callout to send the account data to an external system. But before the webservice is called, you may see the error 'You have uncommitted work pending. Please commit or rollback before calling out.'
To resolve the "You have uncommitted work pending" error in Salesforce Apex, restructure your code so that all callouts are made before any DML operations. It is not possible to perform an explicit commit in Apex. Move callout logic to a separate execution context if needed.
Execute operations in this sequence to avoid the error:
This sequence causes the error:
@future method by placing the @future(callout=true) annotation on the web service method.@future, execute an action to insert the object and then execute the web service callout on the oncomplete event of a command button. Return a PageReference for immediate user feedback. If the callout returns an error, delete the object and return the user to the original page.The following Visualforce page uses oncomplete to trigger the callout after the save action completes, keeping DML and callout in separate execution contexts:
Visualforce Page: The page uses an apex:actionFunction named executeWS bound to the controller action executeWS, and an apex:commandButton with action save and oncomplete="executeWS()".
Controller: The save() method inserts the object. The executeWS() method queries the inserted object, makes the callout, and on success returns a PageReference. If the callout fails, it deletes the object, adds the error message, and returns null.
<apex:actionFunction name="executeWS" action="{!executeWS}"></apex:actionFunction>
<apex:commandButton value="Save" action="{!save}" oncomplete="executeWS()" />
Controller :
public PageReference save() {
insert obj;
}
public PageReference executeWS(){
obj = [SELECT ...];
try{
callout ws;
} catch(System.Exception ex){
delete obj;
ApexPages.addMessages(e);
return null;
}
return new PageReference('/' + id);
}
000385708

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.