SF-PD2 Advanced Developer Fundamentals Practice Question
A developer is building a service class that must call an external REST endpoint and then update a related record. The external callout occasionally exceeds the allowed response time. The developer wants the update to occur only when the callout succeeds, and wants the failure path to be handled cleanly without leaving partial state. Which approach best satisfies these requirements?
⚠ Common exam trap
The trap here is assuming a try-catch around DML and a callout will roll back cleanly, when the real constraint is that callouts must precede DML in the transaction.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Perform the callout, and if it succeeds, perform the DML update; wrap the callout in a try-catch and handle the exception before any DML executes.
The platform forbids making a callout after DML has occurred in the same transaction, so the callout must complete first. Handling the callout in a try-catch and performing the update only after a successful response ensures no partial state exists when the external service times out or errors. The other approaches either violate the callout-after-DML restriction, rely on unreliable rollback behavior, or move the work to an asynchronous context that cannot meet the conditional synchronous requirement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Perform the DML update first, then make the callout, so the record is saved even if the external system is slow to respond.
Why it's wrong here
Once DML has executed in a transaction, Apex prohibits subsequent callouts and throws a runtime error, so this ordering fails at the callout step. It also inverts the requirement that the update happen only when the callout succeeds, since the record would already be committed to the database before the external result is known. The scenario explicitly needs conditional updating, which this order cannot provide.
- ✗
Wrap the callout and the DML update together inside a single try-catch, relying on the transaction rollback to undo the update if the callout fails.
Why it's wrong here
A callout that times out throws an exception that can be caught, but the platform does not roll back DML automatically on an unhandled exception in a way the developer can rely on for this pattern; partial commits and savepoint behavior make the outcome unpredictable. More fundamentally, mixing the callout and DML inside one block does not enforce the required ordering guarantee. The reliable approach is to complete the callout before any DML.
- ✗
Use a future method to perform both the callout and the DML update, since future methods run asynchronously and avoid callout-after-DML errors.
Why it's wrong here
A future method can perform a callout, but it cannot accept a reference to an sObject or perform DML on records passed in as parameters, and it runs in a separate transaction where the original context is lost. The scenario needs a synchronous conditional update based on the callout result. Future methods also cannot guarantee ordering or return the callout result to the calling transaction, so they do not satisfy the requirement.
- ✓
Perform the callout, and if it succeeds, perform the DML update; wrap the callout in a try-catch and handle the exception before any DML executes.
Why this is correct
Apex allows callouts and DML in the same transaction only when the callout completes before the DML begins. By doing the callout first, catching any callout exception, and only then performing the update, the developer guarantees that a failed or timed-out callout never produces a partial database change. This ordering also respects the platform restriction that disallows making a callout after DML has occurred in the same transaction.
About these practice questions
This SF-PD2 question is part of Courseiva's 226-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.