Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer maintains an Apex class that performs a callout to an external REST service and then writes the response into Salesforce records. The callout occasionally takes longer than expected, and the developer wants to ensure the transaction can recover gracefully without leaving partial data. Which approach best addresses this?

⚠ Common exam trap

The trap here is believing a savepoint can undo an external callout, when savepoints only roll back database changes within the current 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 before any DML and use a savepoint so that if the callout or subsequent DML fails, the transaction can roll back the DML.

Because external callouts are not transactional with the database, the reliable pattern is callout first, then savepoint-protected DML with rollback on failure. This keeps the transaction consistent and respects the rule that a callout cannot follow uncommitted DML in the same Apex transaction.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Use Database.setSavepoint() before the callout and Database.rollback() after the callout to undo the external request.

    Why it's wrong here

    A savepoint only rolls back database changes made in the current transaction; it cannot undo an HTTP callout that already reached an external system. Placing the savepoint before the callout is also problematic because uncommitted DML before a callout triggers a callout exception. This option misunderstands what savepoints control.

  • ✗

    Move the callout into a future method and perform the DML in the original synchronous transaction.

    Why it's wrong here

    Future methods run asynchronously and cannot return the response to the synchronous transaction, so the DML would have no data to write. Splitting callout and DML also introduces ordering and duplicate-processing concerns. This approach does not solve partial data; it simply relocates the callout without a recovery strategy.

  • ✓

    Perform the callout before any DML and use a savepoint so that if the callout or subsequent DML fails, the transaction can roll back the DML.

    Why this is correct

    Apex callouts cannot be rolled back, so the safest pattern is to make the callout first, then wrap the DML in a savepoint and roll back if the write fails. This prevents partial data from being committed when the external response is unusable. It also keeps the callout outside any open savepoint scope, which is required because a callout after uncommitted DML in the same transaction causes a System.CalloutException.

  • ✗

    Set a shorter HTTP timeout using the HttpRequest.setTimeout method and rely on the timeout to prevent partial writes.

    Why it's wrong here

    HttpRequest.setTimeout limits how long the client waits for a response, but a timeout does not roll back DML and does not guarantee the external system did not process the request. It also has a maximum value governed by the platform. Shortening the timeout may reduce waiting but does not provide transactional recovery for partial data.

About these practice questions

One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.