Sub topic 5
Sending values back with FlowAttributeChangeEvent
This chapter assumes you can write a basic Lightning web component with @api properties and custom events. It also assumes you can build a simple screen flow in Flow Builder. You should already know how a component receives input values from a flow.
Why a component needs to send values back
A flow passes values into a screen component through @api properties. Changing one of those properties in JavaScript only changes the component's own copy. The flow knows nothing about the change.
To report a value to the flow, the component dispatches a FlowAttributeChangeEvent. The flow then updates the variable that is mapped to that property. Later screens and elements can read the updated variable.
The event
FlowAttributeChangeEvent comes from the lightning/flowSupport module. Its constructor takes two arguments:
- The API name of the property, as declared in the
meta.xmlfile. - The new value.
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', 5));The name must match the name attribute of a <property> element exactly. A misspelled name does nothing and raises no error.
Declaring an output property
The property must be exposed in the bundle's meta.xml. Without a <property> element, Flow Builder has nothing to map the value to.
<?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 quantity property has no role, so it works as both input and output. Use role="outputOnly" when the flow should only read the value. Use role="inputOnly" when the component should never send the value back, as with label above.
Dispatching on every change
Dispatch the event each time the user changes the value, not only when the screen closes. The flow reads the values when the user clicks Next, and it expects them to be current at that moment.
The template shows a number field and calls a handler on each edit:
<template>
<lightning-input
type="number"
label={label}
value={quantity}
min="1"
step="1"
onchange={handleChange}>
</lightning-input>
</template>The handler updates the local property and then reports it:
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.'
};
}
}The handler converts the text to an integer before dispatching. The property type is Integer, so the flow expects a number. If the field is empty, the handler sends undefined, which clears the flow variable. The validate() method at the end is covered in a later chapter.
The flow of data
Common errors
| Symptom | Likely cause |
|---|---|
| The flow variable stays empty | The property name in the event differs from the `meta.xml` name, or the property is `inputOnly`. |
| The value is a string in the flow | The component sent text where the property type is `Integer` or `Boolean`. |
| The value is lost on Back | The component did not read its stored input again when it was rendered. |
| The output is not mapped | No variable is assigned to the property in the screen's component settings. |
Exercise
A component sets this.quantity = 10 in connectedCallback and never dispatches an event. The flow variable mapped to quantity is still empty on the next screen. Why, and how do you fix it?
Show answer
Assigning to an @api property changes only the component's local value. The flow variable updates only when the component dispatches a FlowAttributeChangeEvent. Add this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity)); after the assignment, and confirm that quantity is declared in meta.xml without role="inputOnly".