Skip to main content
Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

Run a Flow Within a Flow

Learning Objectives

After completing this unit, you’ll be able to:

  • Minimize your work by using the Subflow element.
  • Create a flow that runs within another flow.
  • Configure a referenced flow’s variables to pass data back to the parent flow.
Note

This badge is one stop along the way to Flow Builder proficiency. From start to finish, the Build Flows with Flow Builder trail guides you through learning all about Flow Builder. Follow this recommended sequence of badges to build strong process automation skills and become a Flow Builder expert.

The Power of Laziness

Believe it or not, one of the most important qualities that an admin can have is laziness.

You read that right! If you need to make a change in your Salesforce org that affects four different flows, would you rather update four flows or only one? It sounds like magic, but you too can overcome obstacles like unnecessary work through the power of laziness.

For example, Flo’s organization has four flows that post the same message to Chatter. Whenever something about that Chatter post changes, she updates the duplicated element in all four flows. Flo cringes at the thought of doing so much duplicate work. Fortunately, there’s another element, one that we haven’t used yet: the Subflow element. 

Using the Subflow element in a flow (the parent flow) allows you to reference another flow (the child flow) to run it within the current (parent) flow. The idea of making changes in just one flow instead of all four has Flo pretty excited. Plus, with fewer flows, there are fewer opportunities for something to be overlooked or misconfigured.

Flo asks you to build a separate flow for the Chatter post. She plans to replace the duplicate elements in four separate flows by using the new child flow in their place. Then, if something related to the Chatter post changes, Flo can update the single child flow instead of updating all four separate flows.

Flo Smith relaxing on a fancy lounger, being fanned by a palm leaf, thinking about a flow.

Input and Output Variables

The variables in a child flow are special. Have you noticed the Available for input and Available for output settings when you create variables? Those settings enable variables to carry input from the parent flow to the child flow and carry output from the child flow back to the parent flow.

In the New Resource window, the Availability Outside the Flow section has two options: Available for input and Available for output.

But why send data to and from a child flow? In Flo’s example, the child flow posts to Chatter on the parent flow’s triggering record, so the child flow needs that triggering record’s Id. A child flow can’t access the triggering record in the parent flow, so the parent flow needs to send it to the child flow. And if the parent flow needs to know the ID of the Chatter post created by the child flow, the child flow needs to send that ID back to the parent flow.

You define variables as available for input or output in the child flow.

  • To receive data from a parent flow, the child flow needs a variable that’s available for input.
  • To send data back to the parent flow when the child flow finishes, the child flow needs a variable that’s available for output.
  • If you don’t need to send data in either direction, you don’t need any input or output variables.

Diagram corresponding to the preceding description

Create a Subflow Element

To run a flow within another flow, first you create the parent and child flows. Then you add a Subflow element to the parent flow.

Create the Child Flow

  1. Create a flow and select the Autolaunched Flow type.
  2. Create a text variable:
    • API Name: userMentionID
    • Available for input: checked
  1. Create another text variable:
    • API Name: postTargetID
    • Available for input: checked
  1. Create one more text variable:
    • API Name: chatterPostID
    • Available for output: checked
  1. Create a text template:
    • API Name: chatterBody
    • Body: @[{!userMentionID}], please review this record.
    • In the Body editor, change "View as Rich Text" to "View as Plain Text".
  1. Add an Action element:
    • Action: Post to Chatter
    • Label: Chatter Mention Post
    • API Name: Chatter_Mention_Post
    • Message: chatterBody
    • Target Name or ID: postTargetID
    • Under Show advanced options, select Manually assign variables.
    • Feed Item ID: chatterPostID
  1. Save the flow. For Flow Label, enter Post to Chatter.
  2. Activate the flow.

Create the Parent Flow with a Subflow Element

  1. Create a record-triggered flow:
    • Object: Case
    • Trigger the Flow When: A record is created or updated
    • Condition Requirements: All Conditions Are Met (AND)
    • Field: Priority
    • Operator: Equals
    • Value: High
    • When to Run the Flow for Updated Records: Only when a record is updated to meet the condition requirements
    • Optimize the Flow for: Actions and Related Records
  1. Add a Subflow element:
    • Select the Post to Chatter flow.
    • Label: Post to Chatter
    • API Name: Post_to_Chatter
    • Enable the postTargetID field and select Triggering Case > Case ID.
      The Subflow panel, selecting Triggering Case then Owner ID in the userMentionID field.
    • Enable the userMentionID field and select Triggering Case > Owner ID.
      Make sure you scroll down and select Owner ID, not Owner ID (Group) or Owner ID (User).
Note

When a referenced flow has no variables set to be available for input, you won’t see the Set Input Values section.

  1. Save the flow. For Flow Label, enter Case Priority is High.

Now, whenever the record creation needs change, Flo updates only the single child flow. The score is Laziness—1, Overwork—0!

Consider The Following

Keep these quirks in mind when you work with subflows.

  • The Subflow element can reference only Autolaunched flows and screen flows.
  • If you reference an inactive flow, it runs only for users who have the Manage Flows permission.
  • If a child flow has multiple versions, the parent flow runs the child flow’s active version. If a child flow has no active version, the parent flow runs the latest version.

Resources

Hands-on Challenge

+500 points

Get Ready

You’ll be completing this unit in your own hands-on org. Click Launch to get started, or click the name of your org to choose a different one.

Your Challenge

Simplify and Build Upon the Opportunity Product Flow
In the opportunity product creation flow from unit 1, replace repeated elements with a subflow element that references another flow (child flow) multiple times. Also, add another product path to the parent flow. Prework: If you haven’t already completed the challenge in unit 1 (Define Multiple Paths in a Flow), do that now. Otherwise, you won't be able to complete this challenge.
  • Create an autolaunched flow
  • Create a text variable:
    • API Name: ProductKW
    • Available for input: checked

  • Create another text variable:
    • API Name: OpportunityID
    • Available for input: checked

  • Create another text variable:
    • API Name: PricebookID
    • Available for input: checked

  • Add a Get Records element:
    • Label: Get Product
    • API Name: Get_Product
    • Object: Product
    • Condition Requirements: All Conditions Are Met (AND)
      • Field: Name
      • Operator: Contains
      • Value: ProductKW
    • Keep default values for all other settings

  • Add a Get Records element after the Get Product element:
    • Label: Get PBE
    • API Name: Get_PBE
    • Object: Price Book Entry
    • Condition Requirements: All Conditions Are Met (AND)
    • Add a condition requirement:
      • Field: Pricebook2Id
      • Operator: Equals
      • Value: PricebookID
    • Add another condition requirement:
      • Field: Product2Id
      • Operator: Equals
      • Value: Product from Get_Product > Id
    • Keep default values for all other settings

  • Add a Create Records element after the Get PBE element:
    • Label: Create Opp Product
    • API Name: Create_Opp_Product
    • How to set record field values: Manually
    • Object: Opportunity Product
    • Set Field Values:
      • Field: Opportunity ID, Value: OpportunityID
      • Field: Price Book Entry ID, Value: Price Book Entry from Get PBE > Price Book Entry ID
      • Field: Quantity, Value: 1
      • Field: Total Price, Value: Price Book Entry from Get PBE > List Price

  • Save and activate the flow.
    • Label: Subflow-Create Generator Opp Product
    • API Name: Subflow_Create_Generator_Opp_Product

  • Make a new version of the Add Generator Product to Named Opps flow (Open the flow and click Save As New Version.)
  • Delete these elements:
    • Create 1500 Opp Product
    • Create 2000 Opp Product
    • Get 1500 PBE
    • Get 2000 PBE
    • Get 1500 Product
    • Get 2000 Product

  • On the 1500kW path, add a Subflow element:
    • Select the Subflow-Create Generator Opp Product flow
    • Label: Create 1500 Opp Product
    • API Name: Create_1500_Opp_Product
    • Include the OpportunityId field and select Triggering Opportunity > Opportunity ID
    • Include the PricebookID field and select Price Book from Get Pricebook > Price Book ID
    • Include the ProductKW field and enter 1500

  • On the 2000kW path, create a Subflow element:
    • Select the Subflow-Create Generator Opp Product flow
    • Label: Create 2000 Opp Product
    • API Name: Create_2000_Opp_Product
    • Include the OpportunityId field and select Triggering Opportunity > Opportunity ID
    • Include the PricebookID field and select Price Book from Get Pricebook > Price Book ID
    • Include the ProductKW field and enter 2000

  • In the Check Opp Name Decision element, add a new outcome:
    • Label: 1000kW
    • API Name: X1000kW
    • Condition Requirements: All Conditions Are Met (AND)
    • Resource: Triggering Opportunity > Name
    • Operator: Contains
    • Value: 1000

  • On the 1000kW path, create a Subflow element:
    • Select the Subflow-Create Generator Opp Product flow
    • Label: Create 1000 Opp Product
    • API Name: Create_1000_Opp_Product
    • Include the OpportunityId field and select Triggering Opportunity > Opportunity ID
    • Include the PricebookID field and select Price Book from Get Pricebook > Price Book ID
    • Include the ProductKW field and enter 1000

  • Save and activate the flow
Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback