Video Chapters
0:00 Stop Duplicating Workflows! | Create Sub-Workflows in n8n for AI Automation - Part 01
0:00 n8n Sub-Workflows in Action | Build Modular n8n AI Automations - Part 02

n8n Sub-Workflows: Build Modular, Reusable Automations | Complete Guide

Create sub-workflows in n8n to reuse logic across automations, simplify complex workflows, and keep your automation systems maintainable. With examples.

Table of Contents

Overview

In this lesson, you'll learn how to use sub-workflows in n8n to modularize and optimize your automation workflows. Sub-workflows allow you to reuse complex logic across multiple workflows, improving maintainability, scalability, and readability while reducing duplication. By the end of this tutorial, you'll understand how to create, configure, and invoke sub-workflows with the Execute Workflow node, and how to pass data between parent and child workflows effectively.

For more on testing and refining your workflows, consider exploring techniques to pin data in n8n or how to fix AI automation workflows fast in n8n.


What Are Sub-Workflows in n8n?

A sub-workflow is a separate workflow that is called from within another workflow (the parent workflow). This is done using the Execute Workflow node, which lets you:

  • Call another workflow inside your current workflow.
  • Pass input data to the sub-workflow.
  • Receive output data back from the sub-workflow.

Why Use Sub-Workflows?

Using sub-workflows brings several benefits:

  • Reusability: Avoid duplicating the same logic across multiple workflows.
  • Maintainability: Change logic in one place, and all parent workflows using that logic get updated automatically.
  • Scalability: Keep workflows clean, modular, and easier to manage.
  • Readability: Structured workflows are easier to understand and debug.
  • Performance: Segregating logic can optimize execution speed and resource usage.

For detailed information, refer to the n8n Merge node documentation to understand how merging data works in conjunction with sub-workflows.


Building a Support Ticket Handling Workflow with Sub-Workflows

Let's walk through building a support ticket handling system and then refactor it using sub-workflows.

Step 1: Create the Parent Workflow

  1. Add a Webhook Trigger

    • Add a Webhook trigger node to receive incoming support tickets.
    • Use sample/mock data for testing (you can pin data or use the "Edit Data" feature).
    • Example mock data:
    [
      {
        "email": "john.doe@example.com",
        "ticketMessage": "Received damaged product, need replacement urgently"
      },
      {
        "email": "jane.smith@example.com",
        "ticketMessage": "Order not delivered yet"
      }
    ]
    
  2. Fetch Customer Record from Google Sheets

    • Add a Google Sheets node.
    • Set the operation to Get Rows.
    • Select the spreadsheet containing your customer data, e.g., sub workflows mock data.
    • Select the sheet user data.
    • Add a filter where the email column matches the email from the webhook input.
  3. Merge Customer Data and Ticket Data

    • Use a Merge node to combine the data from the webhook and the Google Sheets node.
    • Configure the merge mode to Merge By Field or Append, depending on your data structure.
    • Match on the email field (ensure case sensitivity matches or normalize both).
  4. Calculate VIP Status

    • Add an IF node.
    • Set conditions:
      • order_history > 50 OR
      • average_rating > 4.5
    • Use the "OR" condition so that either condition being true qualifies the customer as VIP.
  5. Route Ticket Based on VIP Status

    • For the true branch (VIP customers):

      • Add a Slack node to send a notification to the VIP support channel.
      • Compose a message using expressions, e.g.,
      {{$json["customer_name"]}} says: {{$json["ticketMessage"]}}
      
    • For the false branch (non-VIP customers):

      • Add another IF node to check the region (e.g., region == "EU").
      • Route tickets to the EU team or US team accordingly.
  6. Log Ticket in Google Sheets

    • Add a Google Sheets node and set operation to Append Row.
    • Select the support tickets sheet in your mock data.
    • Map fields like ticket ID, customer email, issue description, assigned team, priority, and status.
    • For ticket ID, generate a hash of the ticket message to create a unique identifier:
    {{ $json["ticketMessage"] | hash() }}
    

Refactoring with Sub-Workflows

Now that we have a working parent workflow, let's modularize the customer lookup and VIP status calculation into a reusable sub-workflow.

Step 2: Create the Sub-Workflow for Customer Lookup and VIP Calculation

  1. Create a New Workflow

    • Name it something like Sub Workflow: Fetch Customer Details.
    • Add a trigger node of type Execute Workflow (triggered when called by another workflow).
    • Configure it to accept input parameters, e.g., customer_email.
  2. Fetch Customer Data

    • Add a Google Sheets node:
      • Operation: Get Rows.
      • Spreadsheet: sub workflows mock data.
      • Sheet: user data.
      • Filter by email matching customer_email from input.
  3. Calculate VIP Status

    • Add an Set node (or Set Field node) to add calculated fields:

      • isVIPCustomer: Boolean, true if order_history > 50 OR average_rating > 4.5.
      • Use an expression like:
      {{ $json.order_history > 50 || $json.average_rating > 4.5 }}
      
    • Enable the option Include Other Fields to keep all existing customer data.

  4. Return Output

    • The sub-workflow will output the enriched customer data with VIP status.

Step 3: Connect Sub-Workflow to Parent Workflow

  1. Add an Execute Workflow Node in Parent Workflow

    • Select the sub-workflow created above.
    • Configure mode:
      • Run Once for Each Item (if you want to process input data item-wise).
    • Map the input parameter customer_email to the email from your webhook or previous node:
    {{$json["email"]}}
    
  2. Replace Existing Customer Lookup Logic

    • Disconnect the old Google Sheets and merge nodes used for customer lookup.
    • Connect the Execute Workflow node output to the next steps in your workflow.
  3. Test the Workflow

    • Execute the parent workflow.
    • Verify that the sub-workflow is called, and customer data with VIP status is returned.
    • Use the View Subworkflow Execution option in n8n to debug and check the sub-workflow runs.

For more on how to test AI workflows without incurring API costs, see the AI Automation with n8n and OpenAI lesson.


Extending Sub-Workflows for Other Use Cases

You can reuse this customer lookup sub-workflow in other automation workflows, such as:

  • Order Confirmation Workflow: Lookup customer details and send personalized emails based on segmentation.
  • Marketing Campaign Workflow: Filter VIP customers in specific regions and export their data for targeted campaigns.

By centralizing customer evaluation logic in one sub-workflow, you ensure consistency and reduce maintenance overhead.


Practical Example: Sub-Workflow JSON Snippet

Here’s a simplified example of the Set node expression to calculate VIP status inside the sub-workflow:

{
  "nodes": [
    {
      "parameters": {
        "fields": [
          {
            "fieldName": "isVIPCustomer",
            "fieldValue": "={{ $json.order_history > 50 || $json.average_rating > 4.5 }}"
          }
        ],
        "options": {
          "includeAllFields": true
        }
      },
      "name": "Set VIP Status",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [600, 300]
    }
  ]
}

Common Mistakes and Troubleshooting

  • Input Data Mismatch: Ensure the sub-workflow input parameters exactly match the data passed from the parent workflow. Use consistent field names and types.
  • Case Sensitivity in Filters: When filtering Google Sheets data, be mindful of case differences (e.g., Email vs email). Normalize inputs if necessary.
  • Not Waiting for Sub-Workflow Completion: If the Execute Workflow node option Wait for Subworkflow Completion is disabled, subsequent nodes might execute before the sub-workflow finishes, causing missing data.
  • Incorrect Merge Configuration: When merging data from different sources, verify the correct merge mode and matching fields to avoid empty or duplicate records.
  • Sub-Workflow Execution Errors: Use the View Subworkflow Execution feature to inspect inputs, outputs, and errors inside the sub-workflow.

If you encounter issues, reviewing the Fix AI Automation Workflows in n8n lesson can provide helpful debugging strategies.



Quick Reference: Sub-Workflow Setup Cheat Sheet

Step Node / Action Key Settings / Tips
Create Sub-Workflow New Workflow Use Execute Workflow trigger
Accept Input Execute Workflow Trigger Define expected input fields (e.g., customer_email)
Fetch Data Google Sheets (Get Rows) Filter by input parameter
Calculate Values Set Node Use expressions for computed fields (e.g., VIP status)
Return Data Output of Sub-Workflow Ensure all required fields are included
Call Sub-Workflow Execute Workflow Node in Parent Map input fields, enable Wait for Subworkflow Completion
Replace Duplicated Logic Remove old nodes and connect new node Keep workflow clean and modular

By applying these steps, you’ll be able to build efficient, reusable, and modular workflows in n8n, leveraging sub-workflows to streamline your automation projects.

Frequently Asked Questions

Use the Execute Workflow node in your parent workflow to invoke a sub-workflow, allowing you to run separate workflows modularly.

You pass data to a sub-workflow via the Execute Workflow node's input, and retrieve output data from the sub-workflow to use in the parent workflow.

Sub-workflows improve reusability, maintainability, scalability, readability, and can optimize performance by modularizing complex logic.

Yes, sub-workflows are designed to be reusable components that can be invoked by multiple parent workflows to avoid duplication.

You can use the Execute Workflow node's settings to inspect input and output data, and use pinning or test data to verify data passed between workflows.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Get Premium Resources