Sub topic 8
Controlling Next, Back, Pause, and Finish
A screen flow normally shows its own footer with Previous, Next, Pause, and Finish buttons. Sometimes a component needs more control. It might offer its own buttons, move on automatically after a choice, or hide a button the user shouldn't use. This chapter covers the two pieces that make that possible: the availableActions property and the navigation events in lightning/flowSupport.
This chapter assumes you can already:
- Expose a component to Flow Builder with the
lightning__FlowScreentarget. - Declare
@apiproperties for flow inputs and outputs. - Send output values with
FlowAttributeChangeEvent. - Write a
validate()method.
What the flow tells your component
The flow runtime sets a property named availableActions on every component on a screen. It is an array of strings, and each string is a navigation action the flow allows on the current screen. You don't declare it in the .js-meta.xml file. You only add an @api field with that exact name in your class.
| Action string | Meaning | Navigation event |
|---|---|---|
| `NEXT` | The screen can advance to the next element. | `FlowNavigationNextEvent` |
| `BACK` | The user can return to the previous screen. | `FlowNavigationBackEvent` |
| `PAUSE` | The flow can be paused and resumed later. | `FlowNavigationPauseEvent` |
| `FINISH` | This is the last screen, and the flow can end. | `FlowNavigationFinishEvent` |
Which actions appear depends on where the screen sits in the flow and how it is configured:
- The first screen has no
BACK. - A screen with no screen after it gets
FINISHinstead ofNEXT. PAUSEappears only if pausing is enabled for the flow and the screen allows it.- A screen reached after a step that cannot be undone, such as a record create, may not offer
BACKeither.
Your component must not assume any action exists. Read the array, and show only the controls the flow permits.
The navigation events
Each event is a plain CustomEvent subclass with no constructor arguments. You dispatch it from the component, and the flow treats it as if the user clicked the matching footer button.
import {
FlowNavigationNextEvent,
FlowNavigationBackEvent,
FlowNavigationPauseEvent,
FlowNavigationFinishEvent
} from 'lightning/flowSupport';The flow only acts on an event if the action is allowed. Dispatching FlowNavigationPauseEvent on a flow that doesn't support pausing has no effect. Checking availableActions first keeps your component's behavior predictable and lets it hide the control instead of showing a dead button.
A component with its own buttons
This component renders a button for each action the flow allows. A getter per action keeps the template simple, and each handler checks the array again before it dispatches.
import { LightningElement, api } from 'lwc';
import {
FlowNavigationNextEvent,
FlowNavigationBackEvent,
FlowNavigationPauseEvent,
FlowNavigationFinishEvent
} from 'lightning/flowSupport';
export default class StepButtons extends LightningElement {
@api availableActions = [];
get canGoBack() {
return this.availableActions.includes('BACK');
}
get canGoNext() {
return this.availableActions.includes('NEXT');
}
get canPause() {
return this.availableActions.includes('PAUSE');
}
get canFinish() {
return this.availableActions.includes('FINISH');
}
handleBack() {
if (this.canGoBack) {
this.dispatchEvent(new FlowNavigationBackEvent());
}
}
handleNext() {
if (this.canGoNext) {
this.dispatchEvent(new FlowNavigationNextEvent());
}
}
handlePause() {
if (this.canPause) {
this.dispatchEvent(new FlowNavigationPauseEvent());
}
}
handleFinish() {
if (this.canFinish) {
this.dispatchEvent(new FlowNavigationFinishEvent());
}
}
}<template>
<div class="slds-grid slds-grid_align-end slds-var-m-top_medium">
<lightning-button
lwc:if={canGoBack}
label="Back"
variant="neutral"
onclick={handleBack}>
</lightning-button>
<lightning-button
lwc:if={canPause}
label="Save for later"
variant="neutral"
class="slds-var-m-left_x-small"
onclick={handlePause}>
</lightning-button>
<lightning-button
lwc:if={canGoNext}
label="Continue"
variant="brand"
class="slds-var-m-left_x-small"
onclick={handleNext}>
</lightning-button>
<lightning-button
lwc:if={canFinish}
label="Submit"
variant="success"
class="slds-var-m-left_x-small"
onclick={handleFinish}>
</lightning-button>
</div>
</template>The meta file needs only the screen target. There is nothing to declare for availableActions.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Step Buttons</masterLabel>
<targets>
<target>lightning__FlowScreen</target>
</targets>
</LightningComponentBundle>The component never builds a NEXT and FINISH pair at the same time, because the flow never offers both on one screen. The template could therefore show a single primary button, but keeping the four checks separate makes the behavior easy to follow.
Setting outputs before you navigate
When your component moves the flow forward, the flow reads its output values at that moment. If you change an output and navigate in the same handler, report the change first and navigate second.
handleSelect(event) {
this.choice = event.detail.value;
this.dispatchEvent(new FlowAttributeChangeEvent('choice', this.choice));
this.dispatchEvent(new FlowNavigationNextEvent());
}This pattern is useful for auto-advance screens, where picking a card or a radio option immediately moves the user on. Dispatching the events in the opposite order risks the flow leaving the screen with the old value.
Navigation and validation
A navigation event dispatched by your component is meant to behave like the footer button. For Next and Finish, the flow checks the screen before it moves on. This includes the validate() method on your component and the validation of any other fields on the screen. If a check fails, the flow stays on the screen and shows the error message.
Back and Pause don't require valid input. The user can go back or save the flow with half-filled fields. Don't rely on validate() to protect data when the user can pause. Anything you need must be safe to resume from an incomplete state.
Test the exact behavior in your org with an invalid value in the component. Navigation behavior has changed across releases, and a short manual test settles it for your API version.
Using the standard footer instead
You don't have to build buttons. If your component only needs to hide or reorder things, leave the footer in place and let validation do the blocking. Custom navigation makes sense in these cases:
- The user's choice should move the flow forward immediately, with no separate button.
- You need button labels or placement the flow footer can't provide.
- A wizard-style component manages several internal steps and calls Next or Finish only at the end.
If you add your own buttons while the standard footer is still visible, users see two sets of controls. Check the screen's footer settings in Flow Builder and hide the standard buttons when your component takes over.
Edge cases and pitfalls
- Stale arrays.
availableActionscan change after the first render, for example when the user returns to a screen. Use getters, as in the example above. Don't copy the array into a field inconnectedCallback. - Mutating the array. The array is supplied by the flow. Treat it as read-only and don't push to it or sort it in place.
- Missing array. In tests and in Flow Builder previews,
availableActionsmay be undefined. Default it to[]as shown. - Back recreates the screen. When the user goes back and forward again, the flow initializes your component again. Anything stored only in component fields is lost. Store the values the user needs in output properties.
- Double clicks. A fast double click can dispatch the same navigation event twice. Disable your button after the first click if the handler does asynchronous work.
- Pause on a screen that can't pause. The event is ignored. The
PAUSEcheck protects the user from a button that does nothing.
Exercise
A flow screen has a component that shows a set of product cards. Selecting a card should store the card's Id in an output property named productId and move to the next screen with no button. Which two events do you dispatch, in which order, and what should you check before dispatching the second?
Show answer
Dispatch FlowAttributeChangeEvent('productId', id) first, so the flow has the new value, then FlowNavigationNextEvent. Before the second, check that availableActions includes 'NEXT'. On a last screen only 'FINISH' is available, so you would dispatch FlowNavigationFinishEvent instead.
Exercise
Your custom Back button works on every screen except the first, where clicking it does nothing. What is the cause, and how should the component handle it?
Show answer
The first screen has no BACK entry in availableActions, so the flow ignores the event. The component should read availableActions through a getter such as canGoBack and hide the Back button when the value is false.