Sub topic 10
Reusable components with generic sObject types
This chapter assumes you can build a basic flow screen component. You should know how to expose it with lightning__FlowScreen, declare properties in meta.xml, and send values back with FlowAttributeChangeEvent.
Why generic types
A component that accepts only Account records must be rewritten for Contact or Opportunity. A generic sObject type lets the flow builder pick the object when they add the component to a screen. One component then serves any object.
You declare the type in meta.xml with a propertyType element. You then use it in the type attribute of one or more properties.
Declaring the type in meta.xml
<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>Three details matter here:
name="T"is a placeholder. You refer to it as{T}for a single record and{T[]}for a collection.extends="SObject"limits the choice to sObject types.- Every property that uses
{T}or{T[]}shares the same concrete object. If the builder picksContact, both properties becomeContact.
In Flow Builder, the builder sets the object type first. The record properties then accept only variables of that type.
Using the type in the component
The JavaScript side does not know the object at design time. Treat each record as a plain object and avoid hard-coded field names where possible.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class RecordPicker extends LightningElement {
@api records = [];
@api selectedRecord;
get rows() {
return (this.records || []).map((record) => ({
id: record.Id,
label: record.Name
}));
}
handleSelect(event) {
const id = event.currentTarget.dataset.id;
const match = (this.records || []).find((r) => r.Id === id);
this.selectedRecord = match ? { ...match } : undefined;
this.dispatchEvent(
new FlowAttributeChangeEvent('selectedRecord', this.selectedRecord)
);
}
}<template>
<ul>
<template for:each={rows} for:item="row">
<li key={row.id}>
<button data-id={row.id} onclick={handleSelect}>{row.label}</button>
</li>
</template>
</ul>
</template>This sketch assumes the object has a Name field. Not every object does (Case uses Subject, for example). If you need other fields, add a String property that holds the label field name, and read record[this.labelField].
The component copies the record with { ...match } before returning it. Inputs from the flow are read-only, so you must never change records or its items in place.
Common errors
| Symptom | Likely cause |
|---|---|
| Record properties do not appear in Flow Builder | `propertyType` is missing, or the `type` value lacks braces, such as `T[]` instead of `{T[]}` |
| Builder cannot map a variable to the property | The variable's object differs from the selected object type |
| `Cannot assign to read only property` error | The component changed an object received from the flow |
| Field is `undefined` in the component | The flow did not load that field. Check which fields the Get Records element retrieved |
The last row catches many people. The component sees only the fields that the flow's record variable contains. A Get Records element set to "Automatically store all fields" avoids the problem. Otherwise, name the fields you need.
Exercise
A component declares <property name="record" type="{T}"/> and <property name="related" type="{T[]}"/> with one propertyType named T. A builder picks Account for record. Which object type must the related collection hold, and why?
Show answer
It must also hold Account records. Both properties refer to the same placeholder T, so Flow Builder resolves them to a single object type.
Exercise
Why does the sample component return { ...match } instead of match itself?
Show answer
Records received from the flow are read-only. Returning a copy gives the component its own object to send back, and it avoids any attempt to change the original input.