Sub topic 5
Sending values back with FlowAttributeChangeEvent
This chapter covers how a Lightning web component on a screen flow screen returns values to the flow. It assumes you can write an LWC with @api properties, that you know basic Flow Builder concepts (screen flows, variables, screen elements), and that you can dispatch a JavaScript event. The examples extend the quantityPicker component from the earlier chapters on meta.xml and inputs.
Why outputs need an event
A flow passes values into a component by setting its @api properties. The reverse direction does not happen on its own. If your component changes this.quantity, the flow still holds the old value, because the flow runtime cannot observe changes to a JavaScript field.
To return a value, the component dispatches a FlowAttributeChangeEvent. The event names a property and carries its new value. The flow runtime receives it and writes the value into whatever flow variable or resource is mapped to that property.
The event is imported from the lightning/flowSupport module:
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';The constructor takes two arguments:
attributeName: the name of the property, exactly as declared in themeta.xml<property>element. It is case-sensitive.attributeValue: the new value to store in the flow.
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', 5));The data flow
The diagram shows one edit and the path the value takes back to the flow.
Two details matter here. The component's own field and the flow's variable are separate copies. Updating one does not update the other. Also, the event sends the value at the moment of dispatch. It is not a live binding.
A complete example
Here is the component from the topic spine. The meta.xml declares quantity as an input and output property.
<?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>The template shows the current value and calls a handler on every edit.
<template>
<lightning-input
type="number"
label={label}
value={quantity}
min="1"
step="1"
onchange={handleChange}>
</lightning-input>
</template>The JavaScript class dispatches the event from the handler.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class QuantityPicker extends LightningElement {
@api label = 'Quantity';
@api quantity;
handleChange(event) {
const parsed = parseInt(event.detail.value, 10);
this.quantity = Number.isNaN(parsed) ? undefined : parsed;
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));
}
@api validate() {
if (Number.isInteger(this.quantity) && this.quantity > 0) {
return { isValid: true };
}
return {
isValid: false,
errorMessage: 'Enter a whole number greater than zero.'
};
}
}In handleChange, the code does three things in order. It parses the raw string from the input. It stores the result locally so the template and validate() see it. Then it reports the same value to the flow.
Why the handler writes the field first
@api properties set by a parent are meant to be read-only inside the component. On a flow screen, the runtime is the parent. Assigning this.quantity inside the component is tolerated here because the property is both the component's state and the flow's input. The assignment is local, though, and the flow only learns about it through the event. Always do both: assign, then dispatch.
Which properties can send values
Not every property can return a value. Whether the flow accepts the event depends on the property's declaration in meta.xml.
| Declaration | Flow can set it | Event is accepted |
|---|---|---|
| No `role` attribute | Yes | Yes |
| `role="inputOnly"` | Yes | No |
| `role="outputOnly"` | No | Yes |
In the example, label is inputOnly. Dispatching FlowAttributeChangeEvent('label', ...) would have no effect on the flow. The quantity property has no role, so Flow Builder lets the author both assign a value to it and read it back.
Supported value types
The value you pass must match the type in meta.xml. The common mappings are:
| `meta.xml` type | JavaScript value |
|---|---|
| `String` | string |
| `Integer` | number with no fractional part |
| `Boolean` | `true` or `false` |
| `Date` | ISO date string such as `2026-11-30` |
| `DateTime` | ISO date-time string |
| `{T}` | one record object |
| `{T[]}` | array of record objects |
Pass undefined or null to clear a value. The example does this for unparseable text, so a half-typed entry such as an empty box does not leave a stale number in the flow.
Returning records and collections
Generic sObject properties use the same event. A later chapter covers the setup in detail. The meta.xml fragment declares an input collection and an output record.
<targetConfig targets="lightning__FlowScreen">
<propertyType name="T" extends="SObject" label="Object type" description="The object type of the records"/>
<property name="records" type="{T[]}" label="Records" description="Records to display"/>
<property name="selectedRecord" type="{T}" label="Selected record" role="outputOnly"/>
</targetConfig>To return the chosen record, dispatch it by property name:
handleSelect(event) {
const id = event.currentTarget.dataset.id;
this.selectedRecord = this.records.find((r) => r.Id === id);
this.dispatchEvent(
new FlowAttributeChangeEvent('selectedRecord', this.selectedRecord)
);
}Send a new object or array when the content changes. If you mutate an array in place and dispatch the same reference, the flow may not detect a change.
When to dispatch
Dispatch at the moment the user's input changes, not only at the end. The flow reads the stored value when the user clicks Next, and nothing in the runtime asks the component for its outputs at that moment. If you only update a local field and never dispatch, the flow variable keeps its previous value, and validate() may pass while the flow sees stale data.
Common dispatch points:
- An
onchangehandler on a standard input. - A selection or click handler on a list or a tile.
- The end of an asynchronous call such as an imperative Apex result.
connectedCallback(), when the component computes a default value that should reach the flow before the user touches anything.
The last case is easy to miss. If the component shows a default of 1 and the user accepts it, no change event fires, and the flow variable stays empty. Dispatch the default explicitly:
connectedCallback() {
if (this.quantity === undefined) {
this.quantity = 1;
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));
}
}Common errors
- Misspelled property name. The event name argument must match
meta.xmlexactly. A mismatch produces no visible error, and the flow variable simply never changes. - Dispatching without a matching
<property>. An output the component never declared cannot be mapped in Flow Builder. - Wrong type. Sending the string
"5"to anIntegerproperty can fail or be coerced unpredictably. Convert first, ashandleChangedoes withparseInt. - Forgetting the local assignment. Dispatching without updating the field leaves the template and
validate()out of step with what the flow holds. - Expecting the event to navigate.
FlowAttributeChangeEventonly stores a value. Navigation uses separate events, covered in a later chapter. - Testing outside a flow. In a Jest test or on a plain Lightning page, no runtime listens, so nothing happens. Test the dispatch by listening for the event on the element and checking its
detail.
Exercise
A component declares <property name="selectedColor" type="String" role="outputOnly"/>. The user picks a color, the handler runs this.selectedColor = 'red';, and the next flow screen shows an empty variable. What is missing, and where should it go?
Show answer
The component never dispatched the value. It needs this.dispatchEvent(new FlowAttributeChangeEvent('selectedColor', this.selectedColor)); in the handler, right after the assignment, with FlowAttributeChangeEvent imported from lightning/flowSupport. Without the event, the flow keeps its original value.
Exercise
The component shows a default of 1 in the quantity box. The user clicks Next without touching it, and the flow's quantity variable is empty. Why, and how do you fix it?
Show answer
No change event fired, so the flow never received the default. Dispatch a FlowAttributeChangeEvent('quantity', 1) in connectedCallback() when the value is not yet set, after assigning it locally.