A logistics company inserts a Shipment__c record and then, in the same synchronous Apex method, calls an external carrier API with Http.send(). The call fails with "You have uncommitted work pending. Please commit or rollback before calling out." What is the best fix?
- Wrap the DML statement in a try/catch block so the callout can proceed.
- Call Database.setSavepoint() before the insert so the work is isolated.
- Move the callout into a Queueable class that implements Database.AllowsCallouts and enqueue it after the insert.
- Increase the callout timeout with req.setTimeout(120000).
Show answer
Answer: C
Why: Apex cannot make a callout after DML in the same transaction. Either perform the callout before any DML or move it to asynchronous Apex such as a Queueable with Database.AllowsCallouts or a @future(callout=true) method.