Sub topic 2
Exposing a component with the lightning__FlowScreen target
A Lightning web component does not appear in Flow Builder by default. The component bundle has to declare that it can run on a flow screen. This chapter covers the metadata that does that, what each part of it means, and the errors you are most likely to hit when you deploy it.
This chapter assumes you can already write a basic LWC (template, JavaScript class, @api properties), that you have built a simple screen flow, and that you can deploy a bundle with Salesforce DX or the Developer Console. It also assumes you know how custom events work in JavaScript. Later chapters cover the properties in depth. Here the focus is the exposure step.
Where exposure is configured
Every LWC bundle has a configuration file named <componentName>.js-meta.xml. Salesforce reads this file at deploy time to decide where the component can be used. The same file controls exposure to the Lightning App Builder, Experience Builder, and Flow Builder.
Three elements matter for flows:
isExposedcontrols whether the component is visible outside its own namespace and in builder tools.targetslists the places the component can be used.targetConfigsholds the settings for each target, such as the properties Flow Builder shows.
The target for screen flows is lightning__FlowScreen. When it is present and isExposed is true, the component appears in the Custom group of the component list in the Screen element of Flow Builder.
A minimal exposed component
The following file exposes a component called Quantity Picker to screen flows. Save it as quantityPicker.js-meta.xml in the bundle folder.
<?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>Each element does the following:
| Element | Purpose |
|---|---|
| `apiVersion` | The Salesforce API version the component runs against. Use a current version for new components. |
| `isExposed` | When `true`, builders can see and use the component. When `false`, the bundle can still be used from other components but not from Flow Builder. |
| `masterLabel` | The name shown in the Flow Builder component list. If omitted, Flow Builder shows the component's API name. |
| `targets` | The list of places the component is allowed. Here it is only `lightning__FlowScreen`. |
| `targetConfigs` | Per-target settings. The `targets` attribute on each `targetConfig` says which target the settings apply to. |
| `property` | One value exchanged with the flow. Covered in the next chapter. |
The template and JavaScript for this component are short. The template shows a number input:
<template>
<lightning-input
type="number"
label={label}
value={quantity}
min="1"
step="1"
onchange={handleChange}>
</lightning-input>
</template>The JavaScript class holds the values, reports changes, and validates:
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.'
};
}
}You do not need to understand FlowAttributeChangeEvent or validate() yet. They are covered in later chapters. The point for now is that exposing a component is a metadata step, separate from the code that makes it behave well in a flow.
How the targets list and targetConfigs interact
The targets element and the targetConfigs element work together, and mismatches between them cause most exposure problems.
- A target must be listed in
targetsbefore you can configure it intargetConfigs. Configuring a target that is not listed causes a deployment error. - A target can be listed without a
targetConfig. The component will then appear in that tool, but with no configurable properties. - A component can list several targets. For example, you can add
lightning__RecordPagenext tolightning__FlowScreen. Each target then gets its owntargetConfig, so a property that makes sense in a flow does not have to appear on a record page. - The
targetsattribute of atargetConfigcan hold a comma-separated list when two targets share the same configuration.
A component with two targets looks like this:
<targets>
<target>lightning__FlowScreen</target>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="quantity" type="Integer"/>
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<property name="label" type="String"/>
</targetConfig>
</targetConfigs>Sharing a component across targets is convenient, but it raises a design question. Flow properties are values passed in and out, while record page properties are only inputs set by an admin. If the component's logic depends on being able to report values back, check that it behaves sensibly where there is no flow. A call to dispatchEvent with a flow event outside a flow is harmless, but the value goes nowhere.
The component lifecycle on a flow screen
Exposure decides whether the component can be placed on a screen. What happens at runtime is also worth knowing, because it explains behavior you will see when testing.
The flow runtime creates a new instance of the component each time a screen containing it is displayed. When the user moves to another screen, the instance is destroyed. If the user returns with Back, the runtime creates a fresh instance and passes the stored flow values into the @api properties again.
The sequence for one visit to a screen is:
- The flow runtime creates the component and sets its input properties from flow values.
connectedCallback()runs, then the template renders.- The user interacts. The component reports changes with
FlowAttributeChangeEvent. - The user clicks Next or Finish. The runtime calls
validate()if the component defines it. - If validation passes, the flow stores the output values and moves on. The component is destroyed.
Because the instance is recreated, any state that is not stored in a flow variable is lost when the user leaves the screen. Do not keep important values only in a private class field.
Deploying and checking the result
Deploy the bundle with the Salesforce CLI:
sf project deploy start --source-dir force-app/main/default/lwc/quantityPickerThen open a screen flow in Flow Builder, add a Screen element, and look for Quantity Picker under Custom in the component list. Drag it onto the canvas. The properties from targetConfigs appear in the configuration panel on the right, with the labels you gave them.
Common exposure errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Component missing from the Flow Builder list | `isExposed` is `false`, or `lightning__FlowScreen` is not in `targets` | Set both correctly and redeploy. Refresh Flow Builder. |
| Deployment error about an unsupported target or property | A `targetConfig` refers to a target not listed in `targets`, or a `property` uses a type that the target does not support | Add the target to `targets`, or change the property type. |
| Property appears with its API name instead of a friendly label | The `label` attribute is missing on the `property` | Add `label`. |
| Component appears but is not what you expected | The `masterLabel` is missing or was copied from another component | Set a unique `masterLabel`. |
| Old properties still show after an edit | Flow Builder has cached the earlier version | Reload the browser tab. Reopen the flow. |
| Flow using an older version of the component breaks | You removed or renamed a property that the flow already references | Keep property names stable. See the callout below. |
Choosing what to expose
Not every component belongs on a flow screen. Before adding the target, consider the following:
- Expose a component to flows when it collects or displays data that a flow needs, such as a custom picker, a table with row selection, or a signature pad.
- Keep components that only make sense inside a larger parent component unexposed. Set
isExposedtofalse, or leave out the flow target. - Keep each exposed component focused. A component that tries to do several jobs needs many properties, which makes it harder for admins to configure correctly.
- Pick a
masterLabeland property labels for flow builders, not for developers. Admins read them in Flow Builder.
Exercise
A developer adds <targetConfig targets="lightning__FlowScreen"> with two properties to a component's meta.xml, but the <targets> list contains only lightning__RecordPage. What happens when the bundle is deployed, and what two changes would make the component usable on flow screens?
Show answer
Deployment fails, because a targetConfig may only refer to a target that is listed in targets. To fix it, add <target>lightning__FlowScreen</target> to the targets list, and make sure <isExposed> is true so Flow Builder lists the component. After redeploying and refreshing Flow Builder, the component appears under Custom in the Screen element.
Exercise
A user clicks Next on screen 2, then Back, and sees that the text typed into a component's private field is gone, but a value stored in a flow variable is still there. Why?
Show answer
The flow runtime destroys the component instance when the user leaves the screen and creates a new one when the screen is shown again. Private fields are lost with the old instance. Values reported through FlowAttributeChangeEvent are stored in flow variables and passed back into the new instance's @api properties.