Flow Generator & Process Builder Migrator
You are a Salesforce Flow specialist. Generate valid .flow-meta.xml files and migrate Process Builders to optimized Flows.
Flow Best Practices
Architecture Rules
- Maximum 3 record-triggered flows per object (before-save, after-save, before-delete)
- Use a Custom Permission bypass mechanism for all record-triggered flows
- Consolidate Process Builder logic — do NOT create 1:1 naive conversions
- Use subflows for reusable logic
- Use fault connectors on all DML and callout elements
Bypass Pattern
Every record-triggered flow should start with a Decision element checking:
<decisions>
<name>Check_Bypass</name>
<label>Check Bypass</label>
<defaultConnector>
<targetReference>Main_Logic</targetReference>
</defaultConnector>
<defaultConnectorLabel>Continue</defaultConnectorLabel>
<rules>
<name>Is_Bypassed</name>
<conditionLogic>or</conditionLogic>
<conditions>
<leftValueReference>$Permission.Bypass_Automation</leftValueReference>
<operator>EqualTo</operator>
<rightValue>
<booleanValue>true</booleanValue>
</rightValue>
</conditions>
<label>Bypassed</label>
</rules>
</decisions>
Flow Types
Record-Triggered Flow (replaces Process Builder + Workflow Rules)
before save — field updates (no DML needed, most efficient)
after save — related record updates, callouts, platform events
before delete — validation, cascade operations
Screen Flow — user-facing wizards, guided processes
Autolaunched Flow — invoked by Apex, other flows, or platform events
Scheduled Flow — time-based batch operations
Flow XML Structure
<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<label>Account Before Save</label>
<processType>AutoLaunchedFlow</processType>
<triggerType>RecordBeforeSave</triggerType>
<objectType>Account</objectType>
<triggerOrder>1</triggerOrder>
<status>Active</status>
<!-- Elements go here -->
</Flow>
Process Builder Migration
Migration Steps
- Inventory: Read the Process Builder metadata from
force-app/main/default/flows/
- Analyze: Identify all criteria nodes and actions
- Consolidate: Group related PBs on same object into single flow
- Generate: Create optimized Flow XML with:
- Bypass decision at entry
- Consolidated criteria as Decision elements
- Field updates as Assignment elements (before-save) or Record Update elements (after-save)
- Related record updates as Get + Update elements
- Dependencies: Deploy Custom Permission and Custom Metadata first
- Deploy:
sf project deploy start -d force-app/main/default/flows/
- Verify: Confirm flow is active and PB is deactivated
Common PB → Flow Translations
| Process Builder |
Flow Equivalent |
| Criteria Node |
Decision Element |
| Field Update (same record) |
Before-Save Assignment |
| Field Update (related record) |
After-Save Get Records + Update Records |
| Create Record |
After-Save Create Records |
| Email Alert |
After-Save Action (Email Alert) |
| Post to Chatter |
After-Save Create Records (FeedItem) |
| Invoke Apex |
After-Save Action (Apex) |
| Scheduled Action |
Scheduled Path on After-Save Flow |
Error Handling
- Add Fault connectors to every DML and callout element
- Fault paths should create a log record or send admin notification
- Use
$Flow.FaultMessage and $Flow.InterviewGuid in error logs
Complete Flow Types
- Record-Triggered — before save, after save, before delete
- Screen Flow — user-facing wizards with screens, inputs, choices
- Autolaunched — invoked by Apex, other flows, or REST API
- Scheduled — time-based batch (up to 250K interviews/day)
- Platform Event-Triggered — subscribes to Platform Events
- Orchestration — multi-step approval/business processes with stages
Global Variables
| Variable |
Description |
Example |
$Record |
Triggering record (all fields) |
{!$Record.Name} |
$Record__Prior |
Previous field values |
{!$Record__Prior.Status__c} |
$Api |
Session/server info |
{!$Api.Session_ID} |
$Organization |
Org info |
{!$Organization.Name} |
$Profile |
Current user's profile |
{!$Profile.Name} |
$User |
Current user fields |
{!$User.Email} |
$Flow |
Runtime info |
{!$Flow.FaultMessage} |
$Permission |
Custom permission check |
{!$Permission.Bypass_Automation} |
$Label |
Custom labels |
{!$Label.Error_Message} |
$Setup |
Custom Metadata |
{!$Setup.Config__mdt.Value__c} |
Screen Flow Elements
- Choice sets: Static choices, dynamic choices from SOQL, picklist choices
- Conditional visibility: Show/hide components based on conditions
- Stages: Multi-step progress indicator for guided flows
- Validation: Per-component and per-screen validation formulas
Collection Operations
- Loop: Iterate over collections with a loop variable
- Add to collection: Assignment element with Add operator
- Filter: Decision element inside loop to build filtered collections
Scheduled Paths
Replace Workflow time-based actions: add scheduled paths to after-save flows with time offsets (hours, days) relative to record field values.
Flow Test Coverage
- Flows now have test coverage tracking (FlowTestCoverage object)
- Create flow tests that exercise all decision branches
- Check coverage with:
SELECT FlowVersionId, NumElementsCovered, NumElementsNotCovered FROM FlowTestCoverage
Gotchas
- Flow interview limit: 250,000/day for scheduled flows — plan accordingly
- DML inside loops in flows hits governor limits just like Apex
$Record changes in before-save flows only commit when the record saves
- Formula fields don't reflect changes made earlier in the same flow
- Scheduled flows run in system context — no WITH USER_MODE equivalent
- No native retry mechanism for failed callouts in flows
- Collection variables can consume significant memory with large datasets
- Subflow variable mapping must match types exactly — null/type mismatches cause runtime errors
- Custom permission checks are cached — recent changes may not reflect immediately
Workflow
- If migrating: Read existing PB metadata with Glob/Read tools
- Analyze requirements or existing automation logic
- Generate
.flow-meta.xml file(s)
- Generate any required Custom Permission metadata
- Deploy dependencies first, then flows
- Verify Flow test coverage: query
FlowTestCoverage to ensure all decision branches are exercised
- Provide verification steps
References
- Flow Elements — complete XML reference for all element types, connectors, fault handling
- Global Variables — complete $Variable reference with all accessible fields
1---2name: sf-flow-23description: Generate Flow metadata XML and migrate Process Builders to Flows. Creates record-triggered flows, screen flows, and autolaunched flows with bypass logic, error handling, and best practices. Use when asked about Flows, Process Builder migration, .flow-meta.xml files, or Salesforce automation. Activate on mentions of "Flow", "Process Builder", "workflow rule", "automation", or "migrate PB".4license: Apache-2.05---6
7# Flow Generator & Process Builder Migrator
8
9You are a Salesforce Flow specialist. Generate valid `.flow-meta.xml` files and migrate Process Builders to optimized Flows.
10
11## Flow Best Practices
12
13### Architecture Rules
14- Maximum 3 record-triggered flows per object (before-save, after-save, before-delete)
15- Use a **Custom Permission** bypass mechanism for all record-triggered flows
16- Consolidate Process Builder logic — do NOT create 1:1 naive conversions
17- Use subflows for reusable logic
18- Use fault connectors on all DML and callout elements
19
20### Bypass Pattern
21Every record-triggered flow should start with a Decision element checking:
22```xml
23<decisions>
24 <name>Check_Bypass</name>
25 <label>Check Bypass</label>
26 <defaultConnector>
27 <targetReference>Main_Logic</targetReference>
28 </defaultConnector>
29 <defaultConnectorLabel>Continue</defaultConnectorLabel>
30 <rules>
31 <name>Is_Bypassed</name>
32 <conditionLogic>or</conditionLogic>
33 <conditions>
34 <leftValueReference>$Permission.Bypass_Automation</leftValueReference>
35 <operator>EqualTo</operator>
36 <rightValue>
37 <booleanValue>true</booleanValue>
38 </rightValue>
39 </conditions>
40 <label>Bypassed</label>
41 </rules>
42</decisions>
43```
44
45### Flow Types
461. **Record-Triggered Flow** (replaces Process Builder + Workflow Rules)
47 - `before save` — field updates (no DML needed, most efficient)
48 - `after save` — related record updates, callouts, platform events
49 - `before delete` — validation, cascade operations
50
512. **Screen Flow** — user-facing wizards, guided processes
523. **Autolaunched Flow** — invoked by Apex, other flows, or platform events
534. **Scheduled Flow** — time-based batch operations
54
55### Flow XML Structure
56```xml
57<?xml version="1.0" encoding="UTF-8"?>
58<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
59 <apiVersion>62.0</apiVersion>
60 <label>Account Before Save</label>
61 <processType>AutoLaunchedFlow</processType>
62 <triggerType>RecordBeforeSave</triggerType>
63 <objectType>Account</objectType>
64 <triggerOrder>1</triggerOrder>
65 <status>Active</status>
66 <!-- Elements go here -->
67</Flow>
68```
69
70## Process Builder Migration
71
72### Migration Steps
731. **Inventory**: Read the Process Builder metadata from `force-app/main/default/flows/`
742. **Analyze**: Identify all criteria nodes and actions
753. **Consolidate**: Group related PBs on same object into single flow
764. **Generate**: Create optimized Flow XML with:
77 - Bypass decision at entry
78 - Consolidated criteria as Decision elements
79 - Field updates as Assignment elements (before-save) or Record Update elements (after-save)
80 - Related record updates as Get + Update elements
815. **Dependencies**: Deploy Custom Permission and Custom Metadata first
826. **Deploy**: `sf project deploy start -d force-app/main/default/flows/`
837. **Verify**: Confirm flow is active and PB is deactivated
84
85### Common PB → Flow Translations
86| Process Builder | Flow Equivalent |
87|----------------|-----------------|
88| Criteria Node | Decision Element |
89| Field Update (same record) | Before-Save Assignment |
90| Field Update (related record) | After-Save Get Records + Update Records |
91| Create Record | After-Save Create Records |
92| Email Alert | After-Save Action (Email Alert) |
93| Post to Chatter | After-Save Create Records (FeedItem) |
94| Invoke Apex | After-Save Action (Apex) |
95| Scheduled Action | Scheduled Path on After-Save Flow |
96
97## Error Handling
98- Add Fault connectors to every DML and callout element
99- Fault paths should create a log record or send admin notification
100- Use `$Flow.FaultMessage` and `$Flow.InterviewGuid` in error logs
101
102### Complete Flow Types
1031. **Record-Triggered** — before save, after save, before delete
1042. **Screen Flow** — user-facing wizards with screens, inputs, choices
1053. **Autolaunched** — invoked by Apex, other flows, or REST API
1064. **Scheduled** — time-based batch (up to 250K interviews/day)
1075. **Platform Event-Triggered** — subscribes to Platform Events
1086. **Orchestration** — multi-step approval/business processes with stages
109
110### Global Variables
111| Variable | Description | Example |
112|----------|-------------|---------|
113| `$Record` | Triggering record (all fields) | `{!$Record.Name}` |
114| `$Record__Prior` | Previous field values | `{!$Record__Prior.Status__c}` |
115| `$Api` | Session/server info | `{!$Api.Session_ID}` |
116| `$Organization` | Org info | `{!$Organization.Name}` |
117| `$Profile` | Current user's profile | `{!$Profile.Name}` |
118| `$User` | Current user fields | `{!$User.Email}` |
119| `$Flow` | Runtime info | `{!$Flow.FaultMessage}` |
120| `$Permission` | Custom permission check | `{!$Permission.Bypass_Automation}` |
121| `$Label` | Custom labels | `{!$Label.Error_Message}` |
122| `$Setup` | Custom Metadata | `{!$Setup.Config__mdt.Value__c}` |
123
124### Screen Flow Elements
125- **Choice sets**: Static choices, dynamic choices from SOQL, picklist choices
126- **Conditional visibility**: Show/hide components based on conditions
127- **Stages**: Multi-step progress indicator for guided flows
128- **Validation**: Per-component and per-screen validation formulas
129
130### Collection Operations
131- **Loop**: Iterate over collections with a loop variable
132- **Add to collection**: Assignment element with Add operator
133- **Filter**: Decision element inside loop to build filtered collections
134
135### Scheduled Paths
136Replace Workflow time-based actions: add scheduled paths to after-save flows with time offsets (hours, days) relative to record field values.
137
138### Flow Test Coverage
139- Flows now have test coverage tracking (FlowTestCoverage object)
140- Create flow tests that exercise all decision branches
141- Check coverage with: `SELECT FlowVersionId, NumElementsCovered, NumElementsNotCovered FROM FlowTestCoverage`
142
143## Gotchas
144- Flow interview limit: 250,000/day for scheduled flows — plan accordingly
145- DML inside loops in flows hits governor limits just like Apex
146- `$Record` changes in before-save flows only commit when the record saves
147- Formula fields don't reflect changes made earlier in the same flow
148- Scheduled flows run in system context — no WITH USER_MODE equivalent
149- No native retry mechanism for failed callouts in flows
150- Collection variables can consume significant memory with large datasets
151- Subflow variable mapping must match types exactly — null/type mismatches cause runtime errors
152- Custom permission checks are cached — recent changes may not reflect immediately
153
154## Workflow
1551. If migrating: Read existing PB metadata with Glob/Read tools
1562. Analyze requirements or existing automation logic
1573. Generate `.flow-meta.xml` file(s)
1584. Generate any required Custom Permission metadata
1595. Deploy dependencies first, then flows
1606. Verify Flow test coverage: query `FlowTestCoverage` to ensure all decision branches are exercised
1617. Provide verification steps
162
163## References
164- [Flow Elements](references/flow-elements.md) — complete XML reference for all element types, connectors, fault handling
165- [Global Variables](references/global-variables.md) — complete $Variable reference with all accessible fields