Sub topic 2
Sending Values Back with FlowAttributeChangeEvent
This chapter assumes you know basic LWC templates, JavaScript classes, @api properties, custom events, and screen-flow configuration.
A screen-flow LWC receives values from Flow Builder through public properties. When a user changes a value, the component must notify the flow. FlowAttributeChangeEvent is the Salesforce event for that purpose. Pass it the exact API property name and the new value.
Sending a value to the flow
This example keeps editable state in a private property. It does not directly change the public quantity property supplied by the flow.
<template>
<lightning-input
type="number"
label="Quantity"
value={draftQuantity}
min="1"
step="1"
onchange={handleChange}>
</lightning-input>
</template>import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class QuantityPicker extends LightningElement {
_quantity;
draftQuantity;
@api
get quantity() {
return this._quantity;
}
set quantity(value) {
this._quantity = value;
this.draftQuantity = value;
}
handleChange(event) {
const value = Number(event.detail.value);
this.draftQuantity = Number.isInteger(value) ? value : undefined;
this.dispatchEvent(
new FlowAttributeChangeEvent('quantity', this.draftQuantity)
);
}
@api
validate() {
return Number.isInteger(this.draftQuantity) && this.draftQuantity > 0
? { isValid: true }
: {
isValid: false,
errorMessage: 'Enter a whole number greater than zero.'
};
}
}The flow stores the event value in its quantity variable. It can then use that output in later elements.
Adding the component to a screen
In Flow Builder, add the component to a Screen element. Map an initial flow variable to quantity if needed. The component emits a new value whenever the user edits the field.
Blocking navigation with validate()
When the user selects Next or Finish, Flow calls the component’s public validate() method. Return isValid: false to keep the user on the screen. Include errorMessage to explain the problem. Return isValid: true when navigation is allowed.
Exercise
Why should the component dispatch FlowAttributeChangeEvent instead of only changing its local value?
Show answer
The event tells the flow about the new value so later flow elements can use it.