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
Add a Webhook Trigger
- Add a
Webhooktrigger 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" } ]- Add a
Fetch Customer Record from Google Sheets
- Add a
Google Sheetsnode. - 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.
- Add a
Merge Customer Data and Ticket Data
- Use a
Mergenode to combine the data from the webhook and the Google Sheets node. - Configure the merge mode to
Merge By FieldorAppend, depending on your data structure. - Match on the email field (ensure case sensitivity matches or normalize both).
- Use a
Calculate VIP Status
- Add an
IFnode. - Set conditions:
order_history> 50 ORaverage_rating> 4.5
- Use the "OR" condition so that either condition being true qualifies the customer as VIP.
- Add an
Route Ticket Based on VIP Status
For the true branch (VIP customers):
- Add a
Slacknode to send a notification to the VIP support channel. - Compose a message using expressions, e.g.,
{{$json["customer_name"]}} says: {{$json["ticketMessage"]}}- Add a
For the false branch (non-VIP customers):
- Add another
IFnode to check the region (e.g.,region== "EU"). - Route tickets to the EU team or US team accordingly.
- Add another
Log Ticket in Google Sheets
- Add a
Google Sheetsnode and set operation toAppend Row. - Select the
support ticketssheet 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() }}- Add a
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
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.
- Name it something like
Fetch Customer Data
- Add a
Google Sheetsnode:- Operation:
Get Rows. - Spreadsheet:
sub workflows mock data. - Sheet:
user data. - Filter by email matching
customer_emailfrom input.
- Operation:
- Add a
Calculate VIP Status
Add an
Setnode (orSet Fieldnode) to add calculated fields:isVIPCustomer: Boolean, true iforder_history> 50 ORaverage_rating> 4.5.- Use an expression like:
{{ $json.order_history > 50 || $json.average_rating > 4.5 }}Enable the option
Include Other Fieldsto keep all existing customer data.
Return Output
- The sub-workflow will output the enriched customer data with VIP status.
Step 3: Connect Sub-Workflow to Parent Workflow
Add an
Execute WorkflowNode 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_emailto the email from your webhook or previous node:
{{$json["email"]}}Replace Existing Customer Lookup Logic
- Disconnect the old Google Sheets and merge nodes used for customer lookup.
- Connect the
Execute Workflownode output to the next steps in your workflow.
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 Executionoption 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.,
Emailvsemail). Normalize inputs if necessary. - Not Waiting for Sub-Workflow Completion: If the
Execute Workflownode optionWait for Subworkflow Completionis 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 Executionfeature 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.
Useful n8n Documentation Links
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.