# Sub-workflows

> Compose workflows from reusable pieces. workflow.chain calls another workflow and can wait for its result; workflow.trigger receives the call, filters callers and passes the data on. Both runs share a correlation id.

Source: https://docs.sommatic.ai/en/workflows/sub-workflows/ (updated 2026-11-26)

When the same process shows up in several workflows (validate a document, notify a team, archive a case), build it
once as its own workflow and call it from the others. Each piece stays testable and versioned on its own.

## The two nodes

| Node                                                                        | Where                    | What it does                                                                                          |
| --------------------------------------------------------------------------- | ------------------------ | ----------------------------------------------------------------------------------------------------- |
| [`workflow.chain`](https://docs.sommatic.ai/en/nodes/workflow.chain.md)     | In the caller            | Calls the target workflow, in `sync` mode (waits for the result) or `async` mode (continues at once). |
| [`workflow.trigger`](https://docs.sommatic.ai/en/nodes/workflow.trigger.md) | First node of the target | Receives the call, checks the caller and outputs the data it sent.                                    |

## Configure the call

In `workflow.chain`:

- **Target Workflow** (`target_flow_definition_id`): the workflow to call, chosen from a list.
- **Payload** (`payload`): the object to send. Use expressions to build it. Leave it empty to forward the node's input
  (`$json`).
- **Execution Mode** (`execution.mode`): `sync` (default) or `async`.
- **Timeout (ms)** (`execution.timeout_ms`): in `sync` mode, how long the call may go without any sign of progress
  from the target run, 30000 by default.

```json
{
  "target_flow_definition_id": "<flow-definition-id>",
  "payload": {
    "customer_id": "{{ $('Webhook Receiver').item.json.body.data.customer_id }}",
    "requested_at": "{{ $now.toISO() }}"
  },
  "execution": { "mode": "sync", "timeout_ms": 60000 }
}
```

## Receive the call

The target workflow starts with `workflow.trigger`. Its output is the payload the caller sent plus a `chain_context`:

```json
{
  "customer_id": "cus_xyz789",
  "requested_at": "2026-11-26T10:15:00.000-05:00",
  "chain_context": {
    "caller_flow_definition_id": "…",
    "caller_flow_instance_id": "…",
    "caller_flow_slug": "order-intake",
    "caller_node_id": "…",
    "correlation_id": "chain-…"
  }
}
```

The next node reads `{{ $json.customer_id }}`. **Accepted Callers** (`accepted_caller_ids`) limits which workflows may
call this one; leave it empty to accept any caller. A caller that is not on the list makes the trigger leave through
`error`.

## What the caller gets back

`workflow.chain` leaves through `out` with:

| Field            | Content                                                                                                                                                           |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `instance_id`    | The run of the target workflow.                                                                                                                                   |
| `status`         | The final status of the target run in `sync` mode, or `running` in `async` mode.                                                                                  |
| `result`         | In `sync` mode, the target's result: `status`, `outcome_category`, `duration_ms`, `error_summary`, `metrics` and `final_output_envelope`. `null` in `async` mode. |
| `correlation_id` | The id shared by both runs.                                                                                                                                       |

The target's last output is at `result.final_output_envelope.payload`:

```javascript
{{ $json.result.final_output_envelope.payload.approved }}
```

> **Warning: Check the status after a sync call**
>
> `workflow.chain` leaves through `out` whenever the call itself worked, even if the target run ended `failed`. Check
> `{{ $json.status === 'completed' }}` with a [`flow.if`](https://docs.sommatic.ai/en/nodes/flow.if.md) after it. The `error` port covers calls
> that could not run: the target was not found, it has no published version, or the wait timed out.

## Which version runs

The target runs in the same mode as the caller:

- When the caller is a test run (studio, `test` webhook), the target runs its **draft**.
- When the caller runs a published version, the target runs its **default published version**. If the target was
  never published, the call fails with `No published version found for the target workflow`.

Publish the target before you publish a caller that depends on it.

## Traceability

Each call generates a `correlation_id` that both runs keep. The id of the first run of the chain also travels down
every level, so `$execution.id` points to the root run in every sub-workflow. In the **Runs** tab, a run that chained
into others lists them so you can open each one.

## Next steps

- [workflow.chain](https://docs.sommatic.ai/en/nodes/workflow.chain.md): Full configuration and ports.
- [Triggers](https://docs.sommatic.ai/en/workflows/triggers.md): Every way a run can start.
- [Variables](https://docs.sommatic.ai/en/expressions/variables.md): $execution and the ids of a run.
