function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Edwin KEdwin K 

Call external web service after DML operation in Apex

Hi,

 

I want to ask how to prevent this exception error ->

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

 

My case is to insert an object and use the id of the inserted object to be sent to external web service... But I got the exception error aboved, how do I manage to prevent this problem? Any suggestions would be great..

Thanx...

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You'd have to use a Future method. I believe the official reason why you can't callout after a DML operation is that you would cause an inconsistent state if there was an error condition that rolled back the transaction (e.g. you submit an ID to an external source then a later error rolls back the transaction). Instead, place the callout in a Future method, and call that method instead.

All Answers

sfdcfoxsfdcfox

You'd have to use a Future method. I believe the official reason why you can't callout after a DML operation is that you would cause an inconsistent state if there was an error condition that rolled back the transaction (e.g. you submit an ID to an external source then a later error rolls back the transaction). Instead, place the callout in a Future method, and call that method instead.

This was selected as the best answer
Edwin KEdwin K

Thanks for the suggestions.. It makes sense anyway, I will call the future method then..

Thanks a lot...