0

Our system is pretty complex with DML statements, call-outs and future methods everywhere. After adding some more functionality, I ran into the dreaded 'uncommitted work pending' error. It had been a while since I saw one of those. I just could not find out what DML caused this error. Attempts to recreate the error via a unit test were unsuccessful: they all run without errors.

Even after disabling all update and insert statements, I still get this error. It happens after the second call-out during a transaction. What is going on?

1 Answer 1

2

Googling for the error gives lots of suggestions, such as moving the call-out to a future method, or collecting all DMLs and perform them after the call-out.

But I had disabled all DML statements, so that wasn't it. More googling led me to this and this. And that was the problem: not only DML before a call-out will cause this error, also calling a future method before a call-out will cause it.

That also explains why I couldn't reproduce it with a unit test: calls to future methods are handled differently in unit tests compared to using Salesforce interactively.

3
  • 1
    Another common DML-like operation that will cause this issue is calling Database.setSavepoint()
    – Derek F
    Oct 18, 2023 at 13:14
  • @DerekF I never use those, so for me that was not a problem. Oct 18, 2023 at 13:50
  • Other people may use them, though, which is why I made a note of it.
    – Derek F
    Oct 18, 2023 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .