Sub topic 1
How screen flows host Lightning web components
Prerequisites
This chapter assumes you know the following:
- Basic Lightning web component (LWC) structure: an HTML template, a JavaScript class, and
@apiproperties. - Basic Flow Builder skills: screen flows, variables, and screen elements.
- How to deploy a component with Salesforce DX or the Developer Console.
- How custom events work in JavaScript.
What a screen flow is
A screen flow is a Flow Builder flow that talks to a user. It shows screens, collects input, runs logic between screens, and ends when the user clicks Finish. Each screen holds elements such as text fields, choices, and display text. A custom LWC is one more kind of screen element.
Flow Builder lists any component that your org exposes for flows alongside the standard elements. You drag it onto a screen and configure it like a built-in element.
How the host and the component talk
The flow and your component never share variables directly. They exchange values through the component's public properties.
- Inputs: the flow sets
@apiproperties on your component when the screen loads. - Outputs: your component dispatches an event to tell the flow that a property changed.
- Validation: when the user clicks Next or Finish, the flow can call your component's
validate()method. - Navigation: the flow tells the component which navigation actions are available, and the component can ask the flow to move on.
The next chapters cover each of these in turn. For now, the important point is that the flow owns the data, and the component only reads and proposes changes.
Lifecycle on each screen
The component follows the normal LWC lifecycle, with the flow acting as its parent.
- The user reaches the screen. The flow creates a new instance of your component and sets its input properties. Then
connectedCallback()runs and the component renders. - The user interacts with the screen. The component reports changes to the flow through events.
- The user clicks a navigation button. The flow calls
validate()if the component defines it. If the result is invalid, the flow stays on the screen and shows the error message. - If validation passes, the flow reads the current output values, leaves the screen, and removes the component.
If the user goes Back and returns to the screen, the flow creates a new instance. The component's own state is gone, so it must rebuild from the input values the flow passes in. This is why every value you want to keep has to be reported back to the flow as an output.
What you build
A flow screen component has three parts, which the rest of the tutorial builds step by step:
| Part | Purpose |
|---|---|
| `meta.xml` file | Exposes the component to Flow Builder and declares its inputs and outputs |
| JavaScript class | Receives inputs, reports outputs, validates, and navigates |
| HTML template | Renders the user interface |
Common early mistakes
- Expecting values to persist. A component is recreated on each visit to a screen. Treat the flow as the single source of truth.
- Changing a property without telling the flow. Setting
this.quantity = 5updates your component, but the flow's variable stays the same until you dispatch an event. - Forgetting the target. Without the right target in
meta.xml, the component never shows up in Flow Builder, even though it deploys without errors.
Exercise
A user fills in your component, clicks Next, and then clicks Back from the following screen. What does the flow do with your component, and what does this mean for where you keep the user's entry?
Show answer
The flow creates a new instance of the component when the screen is shown again, so any internal state is lost. The entry must be sent to the flow as an output value. The flow then passes it back in as an input property, and the component can rebuild its display from it.