A developer has a test class that creates test data before making a mock callout but now receives a 'You have uncommitted work pending. Please commit or rollback before calling out' error.
Which step should be taken to resolve the error?
When working with Apex tests that involve callouts, it's important to avoid the 'uncommitted work pending' error. This error typically occurs because DML operations (like inserts) are pending when a callout is attempted. Salesforce requires that all callouts are made before any DML operations in a transaction. To avoid this, the pattern is to insert the records before calling Test.startTest(), and then make the callout after this call. Test.startTest() marks the point where your actual test begins, and it resets the governor limits. By making the callout after this point, it ensures that the DML operations are committed and you are within a new execution context regarding governor limits.
Testing HTTP Callouts by Implementing the HttpCalloutMock Interface
Understanding Execution Contexts
Stephen
8 days agoCarey
13 days agoMichell
18 days agoErinn
23 days agoMaryrose
29 days agoBrittney
1 month agoTelma
1 month agoShala
1 month agoBeth
2 months agoBarrie
2 months agoStephanie
2 months agoGilma
2 months agoLai
2 months agoCathrine
3 months agoOrville
3 months agoAntonio
3 months agoVeronika
3 months agoCandida
3 months agoBelen
4 months ago