Courseiva
User Interface →easyMultiple Choice

SF-PD2 User Interface Practice Question

A developer is creating a Lightning Web Component that needs to display a toast message when a record is saved successfully. The developer imports ShowToastEvent from lightning/platformShowToastEvent. Which code correctly dispatches a success toast with the title 'Saved' and message 'Record saved successfully'?

⚠ Common exam trap

The trap here is forgetting to include the variant property, which changes the toast's appearance and semantic meaning.

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

✓

this.dispatchEvent(new ShowToastEvent({ title: 'Saved', message: 'Record saved successfully', variant: 'success' }));

The correct code creates a new ShowToastEvent with an object containing title, message, and variant set to 'success', then dispatches it using this.dispatchEvent. This displays a success toast. The other options either use incorrect syntax, omit the variant, or attempt to call a non-existent static method.

Answer analysis

Option-by-option breakdown

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

  • ✗

    ShowToastEvent.dispatch(this, { title: 'Saved', message: 'Record saved successfully', variant: 'success' });

    Why it's wrong here

    ShowToastEvent does not have a static dispatch method. The correct way is to instantiate the event and dispatch it using this.dispatchEvent. Attempting to call ShowToastEvent.dispatch will result in an error because that method does not exist. The developer must create an instance of ShowToastEvent and dispatch it.

  • ✓

    this.dispatchEvent(new ShowToastEvent({ title: 'Saved', message: 'Record saved successfully', variant: 'success' }));

    Why this is correct

    This code correctly creates a new ShowToastEvent with the required parameters: title, message, and variant. The variant 'success' displays a green success toast. Dispatching the event on the component instance using this.dispatchEvent is the standard way to show a toast in LWC. This is the correct and complete syntax.

  • ✗

    this.dispatchEvent(new ShowToastEvent({ title: 'Saved', message: 'Record saved successfully' }));

    Why it's wrong here

    While this code creates a ShowToastEvent with title and message, it omits the variant property. Without a variant, the toast will default to 'info' (blue) rather than 'success' (green). The developer specifically wants a success toast, so the variant must be included. This option does not meet the requirement.

  • ✗

    this.dispatchEvent(new ShowToastEvent('Saved', 'Record saved successfully', 'success'));

    Why it's wrong here

    The ShowToastEvent constructor expects a single object with properties, not positional arguments. Passing three separate arguments will not work and will likely throw an error or produce an incorrect toast. The correct syntax is to pass an object with title, message, and variant properties.

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.