Sub topic 12
Passing Apex-defined types
This chapter assumes you can already declare properties in meta.xml, receive them as @api properties, and send values back with FlowAttributeChangeEvent. It also assumes you know basic Apex classes.
Why use an Apex-defined type
Flow's built-in types cover text, numbers, dates, and sObjects. Sometimes your data fits none of them. A product option with a label, a code, and a computed price is one example. An Apex-defined type lets you describe that shape once in an Apex class. You can then pass it between a flow, Apex actions, and your component.
In the flow, a variable with the data type Apex-Defined holds the value. Your component receives it as a plain JavaScript object, or as an array of objects for a collection.
Define the Apex class
Flow can only see members that are marked @AuraEnabled. The class itself must be visible to Flow, so declare it public. Use global if it ships in a managed package.
public class ProductOption {
@AuraEnabled public String label;
@AuraEnabled public String value;
@AuraEnabled public Decimal price;
}Members without @AuraEnabled are not exposed. They arrive in the component as missing properties, and no error tells you why.
Declare the property in meta.xml
Reference the class with the apex:// prefix. Add [] for a collection. If the class is in a namespace, write apex://namespace__ProductOption.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Product Chooser</masterLabel>
<targets>
<target>lightning__FlowScreen</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="options" type="apex://ProductOption[]" role="inputOnly" label="Available options"/>
<property name="selected" type="apex://ProductOption" role="outputOnly" label="Selected option"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>In Flow Builder, the options field accepts only a collection variable of type ProductOption. The selected output can be stored only in a single ProductOption variable. Flow Builder enforces both rules.
Use the type in the component
The JavaScript side has no class for ProductOption. You work with objects that have the same field names.
<template>
<lightning-combobox
label="Product"
value={selectedValue}
options={comboOptions}
onchange={handleChange}>
</lightning-combobox>
</template>import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class ProductChooser extends LightningElement {
@api options = [];
@api selected;
get comboOptions() {
return this.options.map((o) => ({
label: `${o.label} (${o.price})`,
value: o.value
}));
}
get selectedValue() {
return this.selected ? this.selected.value : undefined;
}
handleChange(event) {
const match = this.options.find((o) => o.value === event.detail.value);
this.selected = match ? { ...match } : undefined;
this.dispatchEvent(new FlowAttributeChangeEvent('selected', this.selected));
}
@api validate() {
return this.selected
? { isValid: true }
: { isValid: false, errorMessage: 'Choose a product to continue.' };
}
}The handler copies the matching object with the spread operator before sending it. Values from the flow are read-only, so never edit options or one of its items in place.
Common errors
| Symptom | Likely cause |
|---|---|
| The Apex-defined type does not appear in Flow Builder | The class is not `public` or `global`, or the `apex://` name is misspelled |
| A field is `undefined` in the component | The field is missing `@AuraEnabled` |
| Flow Builder rejects the variable you choose | You picked a single value for a collection property, or the reverse |
| "Cannot assign to read only property" | The component edited an input object directly |
| The output stays empty in later screens | The component never dispatched `FlowAttributeChangeEvent` |
Exercise
Your component declares <property name="items" type="apex://ProductOption[]"/>, but the price field is undefined for every item in JavaScript. The class declares public Decimal price;. What is wrong, and how do you fix it?
Show answer
The price field is missing the @AuraEnabled annotation, so Flow does not expose it. Change the declaration to @AuraEnabled public Decimal price; and redeploy the class.