Sub topic 14
Running a flow inside an LWC with lightning-flow
The previous chapters put a component inside a flow. This chapter reverses that: a component hosts a flow. The base component lightning-flow runs a flow in the page and lets your JavaScript pass values in and read results out.
This chapter assumes you can write an LWC with @api properties and handle events. It also assumes you know how to mark a flow variable "Available for input" or "Available for output".
When to embed a flow
Embed a flow when you want custom UI around a guided process. Examples are a modal that runs a flow, a record page component that starts a flow with the current record, or a page that reacts when the flow finishes. For a simple launch button, use a flow action or the standard Flow component on the page. lightning-flow is for cases where you need code to control or react to the flow.
Starting a flow from the template
Set flow-api-name and pass input values through flow-input-variables. The flow starts when the component renders.
<template>
<lightning-flow
flow-api-name="Create_Case"
flow-input-variables={inputVariables}
onstatuschange={handleStatusChange}>
</lightning-flow>
</template>Each input variable is an object with name, type, and value.
import { LightningElement, api } from 'lwc';
export default class CaseLauncher extends LightningElement {
@api recordId;
get inputVariables() {
return [
{ name: 'accountId', type: 'String', value: this.recordId }
];
}
handleStatusChange(event) {
const { status, outputVariables } = event.detail;
if (status === 'FINISHED' || status === 'FINISHED_SCREEN') {
const caseId = outputVariables.find((v) => v.name === 'newCaseId');
console.log('Created case', caseId && caseId.value);
} else if (status === 'ERROR') {
console.error('The flow failed');
}
}
}The name must match the flow variable's API name, and that variable must be marked "Available for input". Likewise, the flow only returns variables marked "Available for output".
Reading status changes
The statuschange event fires as the flow moves between states. event.detail carries the data you need.
| Property | Meaning |
|---|---|
| `status` | `STARTED`, `PAUSED`, `FINISHED`, `FINISHED_SCREEN`, or `ERROR` |
| `outputVariables` | Array of `{ name, dataType, value }` for output variables |
| `flowTitle` | Title of the running flow |
| `guid` | Interview ID, used to resume a paused flow |
FINISHED_SCREEN means the flow ended on a screen with a Finish button. FINISHED means it ended without one. Handle both.
Starting the flow from code
To delay the start, omit flow-api-name and call startFlow() on the element. This suits a flow launched from a button.
handleStart() {
const flow = this.template.querySelector('lightning-flow');
flow.startFlow('Create_Case', [
{ name: 'accountId', type: 'String', value: this.recordId }
]);
}The lightning-flow element must already be in the DOM. Wrap it in lwc:if only when you also handle the timing, or the query returns null.
Common errors
- The flow does not start. Check that it is active, that the API name is correct, and that the user has access to run it.
- Inputs are ignored. The variable name is misspelled, or "Available for input" is not set.
- No outputs arrive. You read
outputVariableson a status other than a finished one, or the variable is not marked for output. - Passing the same array object again does not restart the flow. The flow starts once per rendered element.
Exercise
A host component handles statuschange but never sees the output variable newCaseId when the flow ends. Name two possible causes.
Show answer
The flow variable is not marked "Available for output", or the handler reads outputVariables on a status other than FINISHED or FINISHED_SCREEN. A wrong variable name in the lookup would also cause it.