New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce Certified Platform Developer II (Plat-Dev-301) Exam - Topic 5 Question 122 Discussion

Actual exam question for Salesforce's Salesforce Certified Platform Developer II (Plat-Dev-301) exam
Question #: 122
Topic #: 5
[All Salesforce Certified Platform Developer II (Plat-Dev-301) Questions]

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?

Show Suggested Answer Hide Answer
Suggested Answer: D

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

Contribute your Thoughts:

0/2000 characters
Stephen
3 days ago
I feel like option D is the best choice. It aligns with the order of operations.
upvoted 0 times
...
Carey
8 days ago
This question is tricky! I think option C makes sense.
upvoted 0 times
...
Michell
13 days ago
B sounds like a solid approach too!
upvoted 0 times
...
Erinn
18 days ago
A is definitely not the right choice here.
upvoted 0 times
...
Maryrose
24 days ago
Ah, the joys of Apex testing. At least this question isn't asking about code coverage targets!
upvoted 0 times
...
Brittney
29 days ago
Haha, "uncommitted work pending" - sounds like my personal life! Option B is the clear winner here.
upvoted 0 times
...
Telma
1 month ago
I always get confused with the Test.startTest() and Test.stopTest() methods. This question is a real brain-teaser!
upvoted 0 times
...
Shala
1 month ago
Option B seems like the way to go. Gotta keep those test data and mock callouts in order!
upvoted 0 times
...
Beth
1 month ago
I believe the insertion has to happen before Test.startTest(), but I can't recall if the mock callout should be after or within the test method.
upvoted 0 times
...
Barrie
2 months ago
I’m a bit confused about the @testSetup annotation. Does it really change where the mock callout should be?
upvoted 0 times
...
Stephanie
2 months ago
I think we practiced a similar question where the mock callout had to be after the Test.startTest(). That might be the key here.
upvoted 0 times
...
Gilma
2 months ago
I'm leaning towards C. The error is saying there's uncommitted work, so we need to make sure the insertion and mock callout happen after the Test.startTest(), right? That way everything is wrapped in the test context.
upvoted 0 times
...
Lai
2 months ago
I think D makes more sense, though.
upvoted 0 times
...
Cathrine
2 months ago
Okay, I think I've got this. The key is that the error is about "uncommitted work" - that means we need to make sure any DML operations (like the insertion) are completed before we start the test. So B is the right answer, since it ensures the insertion happens before the test starts.
upvoted 0 times
...
Orville
3 months ago
Option C is the way to go!
upvoted 0 times
...
Antonio
3 months ago
Wait, are we sure about that? Seems tricky.
upvoted 0 times
...
Veronika
3 months ago
I remember something about needing to commit changes before making a callout, but I'm not sure if it was related to the order of operations.
upvoted 0 times
...
Candida
3 months ago
Hmm, I'm a bit confused by this question. I thought the Test.startTest() and Test.stopTest() methods were supposed to wrap the entire test scenario, including any data setup. Why would we need to do the insertion before the startTest()?
upvoted 0 times
...
Belen
3 months ago
I'm pretty sure the answer is B. Inserting the records before the Test.startTest() and doing the mock callout in a @testSetup method seems like the right approach to avoid that error.
upvoted 0 times
...

Save Cancel