Sub topic 6
Adding the component to a screen and using its outputs
The previous chapters built a quantityPicker component. It has a lightning__FlowScreen target, a label input, a quantity property, and a FlowAttributeChangeEvent that reports changes. This chapter moves to the flow side. You will place the component on a screen, set its inputs, and use what it returns in later elements.
This chapter assumes you can create a screen flow with variables and Decision elements, and that you can deploy an LWC bundle to an org with Salesforce DX or another deployment tool.
What Flow Builder reads from the bundle
Flow Builder does not inspect your JavaScript. It reads the bundle's meta.xml file, and everything it shows comes from that file.
Here is the configuration from earlier chapters:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Quantity Picker</masterLabel>
<targets>
<target>lightning__FlowScreen</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="label" type="String" role="inputOnly" label="Field label" default="Quantity"/>
<property name="quantity" type="Integer" label="Quantity"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Each part of this file shows up in the builder:
masterLabelis the name in the component list. If you leave it out, Flow Builder shows the component's API name.labelon apropertyis the field label in the configuration panel.role="inputOnly"hides the property from the list of outputs.role="outputOnly"hides it from the inputs. With norole, the property is both an input and an output.defaultfills the field when someone first adds the component.
Adding the component to a screen
Deploy the bundle first, then follow these steps in Flow Builder.
- Create a screen flow, or open an existing one, and add a Screen element.
- In the component palette on the left, open the Custom section. Your component is listed there under its
masterLabel, "Quantity Picker". - Drag it onto the canvas.
- Select it. The right-hand panel shows an API Name, such as
Quantity_Picker1, and one field for each input property.
The API name matters because it is how the rest of the flow refers to this component instance. You can rename it, and you should give it a meaningful name. If you place the same component twice, each instance gets its own API name and its own set of values.
If the component is missing from the palette, check these causes in order:
| Symptom | Likely cause |
|---|---|
| Not in the Custom list at all | `isExposed` is `false`, the `lightning__FlowScreen` target is missing, or the deployment failed |
| Present, but no properties in the panel | The `property` elements are outside a `targetConfig` for `lightning__FlowScreen` |
| A property is missing from the inputs | Its `role` is `outputOnly` |
| A property is missing from the outputs | Its `role` is `inputOnly` |
| Old configuration still showing | Reload the builder, because it caches the component list |
Setting input values
Each input field accepts one of two things:
- A fixed value, such as typing
Order quantityinto thelabelfield. - A flow resource, such as a variable, a formula, or a record field, chosen from the resource picker or typed as
{!varOrderLabel}.
Flow Builder passes these values to your @api properties when the screen loads. The quantity property is both an input and an output, so you can set a starting value by pointing it at a variable. For example, if {!varSuggestedQty} holds 5, the picker opens with 5.
A value you leave empty is not the same as a value of zero. An unset input reaches your component as undefined or null, so code that reads this.quantity should expect that.
Using the outputs
Whenever your component dispatches FlowAttributeChangeEvent, Flow records the new value. After that you can read it from other parts of the flow in two ways.
Reference the component directly
Any later element can use the value as {!ComponentApiName.propertyName}. For the picker named Quantity_Picker1, the reference is {!Quantity_Picker1.quantity}. The resource picker lists it under the screen element's section.
You can use this reference in many places:
- A Decision element condition, such as
{!Quantity_Picker1.quantity}greater than 100. - An Assignment element that copies the value into a variable.
- A Create Records or Update Records element, as a field value.
- Display text on a later screen, such as
You ordered {!Quantity_Picker1.quantity} items. - A formula resource.
Inside the same screen, other components can also use the value in their own inputs and visibility conditions. That is how one component's output drives another component's input.
Store the output in a variable
By default, the output exists only as a property of the component. If you want it in a variable you control, open the component's Advanced section and select Manually assign variables (advanced). The panel then shows an output field for each property, and you choose a variable of a matching type for each one.
Manual assignment is useful when:
- Several elements need the same value, and one variable keeps the references short.
- You want a variable that can be passed to a subflow or returned as a flow output.
- The same value should survive a later screen that replaces the component.
The same Advanced section has a checkbox for refreshing inputs. When it is selected, Flow passes the current values of input resources to the component again whenever the screen is shown, instead of reusing what the user entered earlier. Use it when a value changed elsewhere in the flow, for example after the user pressed Back and then moved forward again.
An end-to-end example
The following flow uses the picker to route large orders for approval.
- Screen: Choose quantity. It holds
Quantity_Picker1, withlabelset toUnits to orderandquantityleft empty. - Decision: Large order? The outcome "Needs approval" tests whether
{!Quantity_Picker1.quantity}is greater than 100. The default outcome covers everything else. - Screen: Confirm. A display text component shows
You are ordering {!Quantity_Picker1.quantity} units.
The dotted line shows that the confirmation screen reads the same output directly. It does not depend on the Decision element.
The default value gotcha
Your component reports a value only when it dispatches FlowAttributeChangeEvent. A component that starts with a default and never dispatches it leaves the flow with an empty output. This bites when the user accepts the default and clicks Next without touching the field.
Suppose the picker should start at 1. Setting this.quantity = 1 in the class does not tell the flow. Dispatch the event when the component connects:
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class QuantityPicker extends LightningElement {
@api label = 'Quantity';
@api quantity;
connectedCallback() {
if (this.quantity === undefined || this.quantity === null) {
this.quantity = 1;
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));
}
}
}Checking for an existing value first matters. If the flow passed in a value, or the user returned with Back, you should keep it and not overwrite it with the default.
Common problems when reading outputs
- The output is empty on the next element. The component never dispatched the event, or it dispatched under a different name. The first argument of
FlowAttributeChangeEventmust match the propertynameinmeta.xmlexactly, including capitalization. - The value has the wrong type. The event value should match the declared type. Send a number for
Integer, not the string fromevent.detail.value. The picker converts withparseIntfor this reason. - A reference to the component does not appear in the resource picker. The element that uses it comes before the screen in the flow, so the value does not exist yet. Move the element after the screen.
- A renamed component breaks nothing, but old text does. Flow Builder updates references it manages when you rename a component's API name. Check merge fields you typed by hand inside long text.
- Values reset on Back. Flow keeps what the user entered on a screen when they go back. If you saw a reset, check whether the refresh inputs option is selected, or whether your
connectedCallbackoverwrites values.
Exercise
A flow screen contains the Quantity Picker with API name Quantity_Picker1, and a Decision element on the next step checks {!Quantity_Picker1.quantity}. Testing shows the Decision always sees an empty value when the user accepts the starting value of 1 without typing. The component sets this.quantity = 1 in connectedCallback. What is wrong, and how do you fix it?
Show answer
Assigning the @api property changes only what the component displays. Flow is told about new values only by FlowAttributeChangeEvent. Fix it by dispatching new FlowAttributeChangeEvent('quantity', this.quantity) in connectedCallback right after setting the default, and only when no value was passed in. The event name must match the quantity property in meta.xml.
Exercise
You want the picker's output stored in a variable named varQty so that a subflow can receive it. Which setting in Flow Builder do you use, and where is it?
Show answer
Select the component on the screen, open its Advanced section, and select Manually assign variables (advanced). Then choose varQty as the variable for the quantity output. The variable's data type must match the property's type.