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

Asynchronous calls and DML

I have a simple logger framework for creating logs for certain actions.  I started getting "You have uncommitted work pending" due to a callout occuring after I inserted a new log entry.  The board suggestions for solving the problem specified using future calls for the callout.  The failing callout is called by a VF page that is displayed after the log is written.  There is no relationship between the log entry and the subsequent call.


I didn't read anything forbidding an insert statement in a future call, so I use the future call for insert the log entry.  When I executed again, I got the same error.  This was puzzling, so I edited out the code where I execute the insert and ran again and I still got the same errror.


Obviously I don't understand what is really happening.  Does a @future Apex job create a "transaction", or in some other way prevent subsequent callouts?


Any suggestions for alternate approach is appreciated.


If you make a DML statement (insert, upsert, delete, etc) then you can't make any callouts after that statement.


Simply put, make all of your callouts first and then do all of your DML after.  Even then, you are limited to 10 call outs per @future call / run.


You can't make an explicit commit call to the database, apex does that for you after your class execution ends.  The commit call sends the work / DML command to the database.  This is why your error message is telling you that you have uncommitted work pending.