Skip to content

执行子工作流#

¥Execute Sub-workflow

使用 Execute Sub-workflow 节点在运行 n8n 的主机上运行不同的工作流。

¥Use the Execute Sub-workflow node to run a different workflow on the host machine that runs n8n.

节点参数#

¥Node parameters

#

¥Source

选择节点应从何处获取子工作流的信息:

¥Select where the node should get the sub-workflow's information from:

  • 数据库:选择此选项可按 ID 从数据库加载工作流。你还必须输入以下内容之一:

¥Database: Select this option to load the workflow from the database by ID. You must also enter either:

  • 从列表获取:从你账户可用的工作流列表中选择工作流。

    ¥From list: Select the workflow from a list of workflows available to your account.

  • 工作流 ID:输入工作流的 ID。工作流的 URL,包含 /workflow/ 之后的 ID。例如,如果工作流的 URL 为 https://my-n8n-acct.app.n8n.cloud/workflow/abCDE1f6gHiJKL7,则工作流 ID 为 abCDE1f6gHiJKL7

    ¥Workflow ID: Enter the ID for the workflow. The URL of the workflow contains the ID after /workflow/. For example, if the URL of a workflow is https://my-n8n-acct.app.n8n.cloud/workflow/abCDE1f6gHiJKL7, the Workflow ID is abCDE1f6gHiJKL7.

  • 本地文件:选择此选项可从本地保存的 JSON 文件加载工作流。你还必须输入:

¥Local File: Select this option to load the workflow from a locally saved JSON file. You must also enter:

  • 工作流路径:输入要让节点执行的本地 JSON 工作流文件的路径。

    ¥Workflow Path: Enter the path to the local JSON workflow file you want the node to execute.

  • 参数:选择此选项可从参数加载工作流。你还必须输入:

¥Parameter: Select this option to load the workflow from a parameter. You must also enter:

  • 工作流 JSON:输入要节点执行的 JSON 代码。

    ¥Workflow JSON: Enter the JSON code you want the node to execute.

  • URL:选择此选项可从 URL 加载工作流。你还必须输入:

¥URL: Select this option to load the workflow from a URL. You must also enter:

  • 工作流 URL:输入你要从中加载工作流的 URL。

    ¥Workflow URL: Enter the URL you want to load the workflow from.

工作流输入#

¥Workflow Inputs

如果你使用数据库和“来自列表”选项选择子工作流,则子工作流的输入项将自动显示,你可以填写或映射值。

¥If you select a sub-workflow using the database and From list options, the sub-workflow's input items will automatically display, ready for you to fill in or map values.

你可以选择移除请求的输入项,在这种情况下,子工作流将接收 null 作为该项的值。你还可以启用“尝试转换类型”功能,以便自动将数据转换为子工作流项请求的类型。

¥You can optionally remove requested input items, in which case the sub-workflow receives null as the item's value. You can also enable Attempt to convert types to try to automatically convert data to the sub-workflow item's requested type.

如果子工作流的“工作流输入触发器”节点使用 "接受所有数据" 输入数据模式,则输入项不会显示。

¥Input items won't appear if the sub-workflow's Workflow Input Trigger node uses the "Accept all data" input data mode.

模式#

¥Mode

使用此参数控制节点的执行模式。选择以下选项:

¥Use this parameter to control the execution mode for the node. Choose from these options:

  • 对所有项目运行一次:将所有输入项传递到节点的单个执行中。

¥Run once with all items: Pass all input items into a single execution of the node.

  • 对每个项目运行一次:依次对每个输入项执行一次节点。

¥Run once for each item: Execute the node once for each input item in turn.

节点选项#

¥Node options

此节点包含一个选项:等待子工作流完成。你可以控制主工作流是否应等待子工作流完成后再继续执行下一步(已启用),或者主工作流是否应立即继续执行(已禁用)。

¥This node includes one option: Wait for Sub-Workflow Completion. This lets you control whether the main workflow should wait for the sub-workflow's completion before moving on to the next step (turned on) or whether the main workflow should continue without waiting (turned off).

模板和示例#

¥Templates and examples

Template widget placeholder.

设置并使用子工作流#

¥Set up and use a sub-workflow

本节逐步介绍如何设置父工作流和子工作流。

¥This section walks through setting up both the parent workflow and sub-workflow.

Create the sub-workflow#

  1. Create a new workflow.

    Create sub-workflows from existing workflows

    You can optionally create a sub-workflow directly from an existing parent workflow using the Execute Sub-workflow node. In the node, select the Database and From list options and select Create a sub-workflow in the list.

    You can also extract selected nodes directly using Sub-workflow conversion in the context menu.

  2. Optional: configure which workflows can call the sub-workflow:

    1. Select the Options Options menu menu > Settings. n8n opens the Workflow settings modal.
    2. Change the This workflow can be called by setting. Refer to Workflow settings for more information on configuring your workflows.
  3. Add the Execute Sub-workflow trigger node (if you are searching under trigger nodes, this is also titled When Executed by Another Workflow).
  4. Set the Input data mode to choose how you will define the sub-workflow's input data:
    • Define using fields below: Choose this mode to define individual input names and data types that the calling workflow needs to provide. The Execute Sub-workflow node or Call n8n Workflow Tool node in the calling workflow will automatically pull in the fields defined here.
    • Define using JSON example: Choose this mode to provide an example JSON object that demonstrates the expected input items and their types.
    • Accept all data: Choose this mode to accept all data unconditionally. The sub-workflow won't define any required input items. This sub-workflow must handle any input inconsistencies or missing values.
  5. Add other nodes as needed to build your sub-workflow functionality.
  6. Save the sub-workflow.

Sub-workflow mustn't contain errors

If there are errors in the sub-workflow, the parent workflow can't trigger it.

Load data into sub-workflow before building

This requires the ability to load data from previous executions, which is available on n8n Cloud and registered Community plans.

If you want to load data into your sub-workflow to use while building it:

  1. Create the sub-workflow and add the Execute Sub-workflow Trigger.
  2. Set the node's Input data mode to Accept all data or define the input items using fields or JSON if they're already known.
  3. In the sub-workflow settings, set Save successful production executions to Save.
  4. Skip ahead to setting up the parent workflow, and run it.
  5. Follow the steps to load data from previous executions.
  6. Adjust the Input data mode to match the input sent by the parent workflow if necessary.

You can now pin example data in the trigger node, enabling you to work with real data while configuring the rest of the workflow.

Call the sub-workflow#

  1. Open the workflow where you want to call the sub-workflow.
  2. Add the Execute Sub-workflow node.
  3. In the Execute Sub-workflow node, set the sub-workflow you want to call. You can choose to call the workflow by ID, load a workflow from a local file, add workflow JSON as a parameter in the node, or target a workflow by URL.

    Find your workflow ID

    Your sub-workflow's ID is the alphanumeric string at the end of its URL.

  4. Fill in the required input items defined by the sub-workflow.

  5. Save your workflow.

When your workflow executes, it will send data to the sub-workflow, and run it.

You can follow the execution flow from the parent workflow to the sub-workflow by opening the Execute Sub-workflow node and selecting the View sub-execution link. Likewise, the sub-workflow's execution contains a link back to the parent workflow's execution to navigate in the other direction.

数据如何在工作流之间传递#

¥How data passes between workflows

As an example, imagine you have an Execute Sub-workflow node in Workflow A. The Execute Sub-workflow node calls another workflow called Workflow B:

  1. The Execute Sub-workflow node passes the data to the Execute Sub-workflow Trigger node (titled "When executed by another node" in the canvas) of Workflow B.
  2. The last node of Workflow B sends the data back to the Execute Sub-workflow node in Workflow A.