Courseiva
User Interface →hardMultiple Choice

SF-PD2 User Interface Practice Question

A developer has a Lightning Web Component that must call an imperative Apex method to save a record, then immediately display a toast confirming success only if the save actually succeeded. The Apex method returns a wrapper with a Boolean success flag and a message. Which imperative call pattern correctly handles both the success and error paths?

⚠ Common exam trap

The trap here is treating the imperative Apex call as synchronous and showing the success toast on the line immediately after the invocation instead of inside the resolved Promise handler.

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

✓

Use .then() to read the wrapper's success flag and conditionally fire the success toast, and .catch() to handle any Apex exception with an error toast.

Imperative Apex calls return Promises, so the response must be consumed asynchronously. Reading the wrapper's success flag inside .then() ensures the toast only fires on a genuine success, while .catch() isolates server exceptions for an error toast. Showing the toast synchronously, using a wire adapter for a save, or relying on a fixed timeout all break the asynchronous contract and produce toasts that do not reflect the real outcome.

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 .then() to read the wrapper's success flag and conditionally fire the success toast, and .catch() to handle any Apex exception with an error toast.

    Why this is correct

    Imperative Apex calls return a Promise, so .then() receives the resolved wrapper, letting the developer check the success Boolean before showing the success toast. .catch() receives server-side Apex exceptions, enabling a distinct error toast. This separation correctly distinguishes a business-level failure (success false) from a runtime exception, satisfying the requirement that the toast appears only when the save truly succeeded.

  • ✗

    Wrap the imperative call in a wire adapter and use the wired property's data and error values to drive the toasts.

    Why it's wrong here

    Wire adapters are for reactive, cacheable data reads, not for imperative save operations triggered by user action. A save must not run automatically when the component loads or when reactive parameters change, which is exactly what a wire adapter does. Using wire here would fire the save at unpredictable times and cannot be conditionally invoked from a button handler, so the pattern is fundamentally wrong for a save-then-toast flow.

  • ✗

    Invoke the Apex method and use a setTimeout of 500 milliseconds before showing the toast, assuming the server will have responded by then.

    Why it's wrong here

    Timing the server response with setTimeout is unreliable; network latency or Apex processing time can exceed any fixed delay, and the toast could fire before or after the actual save completes. This approach also ignores the returned success flag and any exception, so failures would still show a success toast. It introduces a race condition rather than handling the asynchronous contract properly.

  • ✗

    Call the Apex method inside a try/catch, and show the success toast in the try block right after the invocation line without inspecting the resolved value.

    Why it's wrong here

    An imperative Apex call returns a Promise; the code after the invocation line runs before the server responds. Showing the toast there means it appears regardless of the actual outcome, including when the save fails or the Apex method throws. The resolved value must be inspected inside .then() or after an await, so this pattern reports success prematurely and misleads the user.

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 →

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.