Sub topic 4
Receiving flow values in your component
This chapter assumes you know basic LWC templates, @api properties, and how to add a screen element in Flow Builder. It also assumes the component is already exposed with the lightning__FlowScreen target and has its properties declared in the .js-meta.xml file.
How inputs reach the component
Flow Builder shows each <property> from targetConfig as a field on the component's configuration panel. The admin types a value or picks a flow variable. When the screen loads, the flow sets the matching @api property on your component instance.
The meta.xml name and the JavaScript property name must match exactly, including case. Without a matching @api property, the flow has nothing to set and the value never arrives.
import { LightningElement, api } from 'lwc';
export default class QuantityPicker extends LightningElement {
@api label = 'Quantity';
@api quantity;
}With this class and the meta.xml from the earlier chapter, an admin can set label to a text literal and quantity to a number variable. The template then reads {label} and {quantity} like any other reactive property.
Data types
The type attribute in meta.xml controls what Flow Builder offers and what JavaScript receives.
| meta.xml type | JavaScript value |
|---|---|
| `String` | string |
| `Integer` | number |
| `Boolean` | boolean |
| `Date`, `DateTime` | ISO string |
| `{T}` (generic sObject) | object |
| `{T[]}` | array of objects |
An empty flow variable arrives as null or undefined. Code that uses a value should handle both.
When values arrive
The flow assigns properties before your component renders, and it can assign them again later, for example when the user navigates Back and returns. Do not assume that connectedCallback() is the only place to read an input. If you need to react when a value changes, use a setter.
import { LightningElement, api } from 'lwc';
export default class RecordViewer extends LightningElement {
_record;
displayName = '';
@api
get record() {
return this._record;
}
set record(value) {
this._record = value;
this.displayName = value ? value.Name : '';
}
}The setter runs each time the flow supplies a value. It also runs when the value is empty, so guard against null.
Inputs are read-only
Objects and arrays passed from the flow are read-only. Assigning to a field of an incoming record throws an error in the console, and the change never reaches the flow. To edit a record, copy it first, then send the copy back with FlowAttributeChangeEvent, which the next chapter covers.
handleRename(event) {
const copy = { ...this._record, Name: event.detail.value };
this._record = copy;
}Use a spread copy for flat records. Use structuredClone() when the data has nested objects.
Reading inputs with default values
You can give an @api property a default in JavaScript, as label = 'Quantity' does above. The meta.xml default attribute sets the starting value shown in Flow Builder. If the admin leaves the field blank, the flow may pass nothing, so JavaScript defaults still act as a safety net.
Common errors
- The value never shows up. The property name differs from the meta.xml name, or the property has no
@apidecorator. - "Cannot assign to read only property." The code mutates an incoming object or array. Copy it first.
- The component shows a stale value after Back. The code reads the input once in
connectedCallback(). Use a setter or read the property at render time. - A date shows as a string. Date inputs arrive as ISO strings. Parse them with
new Date(value)when you need date logic. - A role of
outputOnlyproperty stays empty. The flow does not send outputOnly properties into the component. Mark a property as both input and output by leavingroleoff.
Exercise
A flow passes a Contact record to your component's contact property. Your handler runs this.contact.Email = newEmail; and fails. Why does it fail, and how do you fix it?
Show answer
Records passed from the flow are read-only, so assigning to one of their fields throws an error. Copy the record first, for example const updated = { ...this.contact, Email: newEmail };, work with the copy, and report the result to the flow with FlowAttributeChangeEvent.