Sub topic 18
Flow metadata and moving flows between orgs
This chapter assumes you can build and activate an autolaunched or schedule-triggered flow and that you know what a sandbox is. Basic Salesforce DX or Metadata API familiarity helps, but you can follow along without it. You will see how a flow is stored as metadata, how versions behave during retrieve and deploy, and how to move a flow from a sandbox to production.
How a flow is stored
A flow is metadata, not data. In the Metadata API it appears as two related types:
| Metadata type | What it holds | Source file |
|---|---|---|
| `Flow` | One version of the flow: elements, variables, resources, and the start definition | `flows/Close_Stale_Cases.flow-meta.xml` |
| `FlowDefinition` | Which version is active, by `activeVersionNumber` | `flowDefinitions/Close_Stale_Cases.flowDefinition-meta.xml` |
Each time you save a flow in Flow Builder, Salesforce creates a new Flow version in the org. The source file does not contain a version number. The file holds one version, and the org decides which one that is.
Here is a trimmed autolaunched flow. It has no trigger, one input variable, and one output variable. It can be deployed as a draft.
<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<environments>Default</environments>
<interviewLabel>Close Stale Cases {!$Flow.CurrentDateTime}</interviewLabel>
<label>Close Stale Cases</label>
<processType>AutoLaunchedFlow</processType>
<start>
<locationX>50</locationX>
<locationY>0</locationY>
</start>
<status>Draft</status>
<variables>
<name>accountId</name>
<dataType>String</dataType>
<isCollection>false</isCollection>
<isInput>true</isInput>
<isOutput>false</isOutput>
</variables>
<variables>
<name>closedCount</name>
<dataType>Number</dataType>
<isCollection>false</isCollection>
<isInput>false</isInput>
<isOutput>true</isOutput>
<scale>0</scale>
</variables>
</Flow>Three parts of this file matter for deployment:
processTypeisAutoLaunchedFlowhere. A schedule-triggered flow has the sameprocessType, and the difference lives in thestartelement.startholds the trigger. For a scheduled flow it contains the object, the filters, and thescheduleblock you saw in the schedule chapter:frequency,startDate,startTime, andtriggerTypeset toScheduled. The schedule travels with the flow file.statusisDraft,Active,Obsolete, orInvalidDraft. The status in the file is a request. The target org's settings decide whether it is honored (see below).
What retrieve and deploy do with versions
Retrieve and deploy are not symmetric.
- Retrieve returns one version per flow. This is the active version, or the latest version if none is active. You never get the full version history.
- Deploy creates a new version in the target org each time the file differs from what is there. It does not overwrite an existing version.
- Version numbers can differ between orgs. Version 7 in a sandbox may become version 3 in production. Never refer to flows by version number in your processes.
Correction: the diagram must be placed in a properly formed fence. Here it is.
Retrieving and deploying with Salesforce CLI
Use the sf CLI to move a flow. The first command pulls the flow from a sandbox, and the second deploys it to a target org.
sf project retrieve start --metadata Flow:Close_Stale_Cases --target-org dev-sandbox
sf project deploy start --metadata Flow:Close_Stale_Cases --target-org uat-sandbox --test-level NoTestRunTo move the active-version setting as well, include the FlowDefinition:
sf project deploy start \
--metadata Flow:Close_Stale_Cases \
--metadata FlowDefinition:Close_Stale_Cases \
--target-org uat-sandboxIf you work from a manifest instead, use a package.xml. Both entries below name the flow by its API name.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Close_Stale_Cases</members>
<name>Flow</name>
</types>
<types>
<members>Close_Stale_Cases</members>
<name>FlowDefinition</name>
</types>
<version>60.0</version>
</Package>You can deploy it with sf project deploy start --manifest package.xml --target-org prod.
Activation in the target org
A deployed flow file with <status>Active</status> does not always become active.
- In sandboxes and Developer Edition orgs, an active flow normally deploys as active.
- In production, Salesforce deploys flows as inactive unless the org setting Deploy processes and flows as active is on (Setup, Process Automation Settings). Without it, the new version arrives inactive and you must activate it by hand.
- Turn that setting on only when your release process has reviewed the flow. An active scheduled flow starts running at its next scheduled time without further action.
For a scheduled flow, check three things after deploying:
- The flow shows the version you expect as active.
- The schedule appears in Setup under Scheduled Jobs, with the right next run time.
- The start date is not in the past, or the first run will not happen when you expect.
Dependencies that the flow file does not carry
A flow file references other metadata by name. Deploying the flow alone fails, or produces a broken flow, when the target org lacks these:
- Custom objects and fields the flow reads or writes.
- Subflows. A parent flow that calls
Close_Stale_Casesneeds that flow in the target, and the subflow must be active if the parent is active. - Apex classes used by invocable actions, and custom metadata types or labels the flow reads.
- Permission sets, if the running user needs access to the new fields.
- Record type and picklist values used in filters or Decision conditions.
Deploy these in the same package, or earlier. When a deploy fails, the error message names the missing component. Fix that component first rather than editing the flow XML.
Removing a flow
You cannot delete an active flow. Deactivate it first. To remove a flow from a target org by deployment, list it in destructiveChanges.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Close_Stale_Cases</members>
<name>Flow</name>
</types>
</Package>A destructive change removes every version of the flow, not just the latest. A flow that another flow calls as a subflow blocks the deletion until the caller is changed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Deployed flow is inactive in production | The Deploy processes and flows as active setting is off | Activate it manually, or enable the setting for the release |
| Deploy fails with a missing field or object | The dependency was not in the package or the target org | Add it to the package or deploy it first |
| Parent flow fails to deploy | The subflow is missing or inactive in the target | Deploy and activate the subflow first |
| Scheduled flow never runs after deploy | Start date in the past, or the version is inactive | Check the active version and the schedule in Scheduled Jobs |
| Version number differs between orgs | Each org numbers its own versions | Track flows by API name, not version |
| Cannot delete a flow | The flow is active, or another flow calls it | Deactivate it and remove the caller first |
Exercise
You deploy a scheduled flow from a sandbox to production. The file says status Active, but the flow never runs. Name two things to check and say where.
Show answer
First, check whether the new version is active. Production deploys flows as inactive unless Deploy processes and flows as active is on in Process Automation Settings, so look at the flow's versions in Flow Builder or Setup and activate it if needed. Second, check Scheduled Jobs in Setup for the flow's schedule and next run time, and confirm that the start date in the schedule is not in the past.
Exercise
A parent flow calls the subflow CloseStaleCases. You deploy only the parent to a UAT sandbox and the deployment fails. What is the cause, and what is the fix?
Show answer
The subflow does not exist (or is not active) in the target org, and the parent flow depends on it by API name. Deploy and activate CloseStaleCases first, or include it in the same package, then deploy the parent.