Sub topic 9
Testing and Debugging in Flow Builder
This chapter assumes you can build a basic LWC, you know Flow Builder screens and variables, and you can deploy components with Salesforce DX or the Developer Console. You will learn how to run a flow that uses your component, find out why a value is wrong, and avoid the errors that come up most often.
How to run your component
A flow screen component only runs inside a flow. Jest can test the component class on its own, but only a real flow run shows how Flow passes values to it and reads them back. The usual loop looks like this:
- Deploy the component bundle to your org.
- Open the flow in Flow Builder and add the component to a screen.
- Click Debug to run the flow in the builder, or Run to run the saved version.
- Change the code, redeploy, and refresh Flow Builder.
Flow Builder caches component metadata. If a new property is missing from the screen editor, reload the browser tab before you look for other causes.
Check values with the debug panel
When you debug a flow, the panel on the right lists each element that ran and the variables after it. After a screen, look for the flow variable you mapped to your component's output.
- If the variable is empty, your component did not dispatch
FlowAttributeChangeEvent, or it used the wrong property name. - If the variable holds an old value, the event was dispatched before the user's last edit.
- If the variable is correct but a later decision goes the wrong way, the flow logic is the problem, not the component.
Compare the name in the event with the name in the meta.xml. They must match exactly, including case.
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));Log from the component
You can add console.log calls and read them in the browser developer tools. Open the console while the flow runs. A temporary log in each handler shows the order of events.
handleChange(event) {
console.log('change', event.detail.value);
const parsed = parseInt(event.detail.value, 10);
this.quantity = Number.isNaN(parsed) ? undefined : parsed;
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));
}Log inside validate() too. Flow calls it when the user clicks Next or Finish, so the log tells you whether the call happens and what it returns. Remove the logs before you deploy to production.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Component is not in the screen editor | Missing `lightning__FlowScreen` target, or `isExposed` is false | Check the meta.xml and redeploy |
| Property is missing in the builder | Property not declared in `targetConfig` | Add a `property` element and reload the tab |
| Output is always empty | No `FlowAttributeChangeEvent`, or a mismatched name | Dispatch the event with the exact property name |
| Next does nothing | `validate()` returns `isValid: false` | Return a clear `errorMessage` and log the result |
| Error about a read-only value | Code changed an object or array received from the flow | Copy the value first, then change the copy |
| Navigation button does nothing | The action is not in `availableActions` | Check the list before you dispatch the event |
The read-only error deserves a closer look. Values passed in from the flow are immutable inside the component. To edit a record or collection, make a copy and send the copy back as an output.
const editable = { ...this.record };
editable.Name = 'New name';
this.dispatchEvent(new FlowAttributeChangeEvent('record', editable));Flow of a debug session
Test the logic with Jest
Keep the logic that does not depend on Flow in small methods, then test them with Jest. You can also check that the component dispatches the right event by listening for it.
import { createElement } from 'lwc';
import QuantityPicker from 'c/quantityPicker';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
describe('c-quantity-picker', () => {
it('rejects zero', () => {
const element = createElement('c-quantity-picker', { is: QuantityPicker });
element.quantity = 0;
document.body.appendChild(element);
expect(element.validate().isValid).toBe(false);
});
it('accepts a positive whole number', () => {
const element = createElement('c-quantity-picker', { is: QuantityPicker });
element.quantity = 3;
document.body.appendChild(element);
expect(element.validate().isValid).toBe(true);
});
});Jest does not run a real flow, so a passing test does not prove that the flow wiring is right. Use Jest for validation rules and formatting, and use Debug for the connection between flow and component.
Exercise
Your component shows the right number on screen, but after the user clicks Next the flow variable mapped to quantity is empty. Name two things you would check.
Show answer
First, check that the component dispatches a FlowAttributeChangeEvent in the change handler. Second, check that the name in the event ('quantity') matches the property name in the meta.xml exactly. You can confirm both by running the flow in Debug and reading the variable value after the screen.