# Branch shape

## Introduction

The `branch` shape allows you to add shapes to multiple, distinct paths in a process flow, which are executed sequentially. Paths are executed one at a time, in the configured sequence, using the same incoming payload.&#x20;

For complex scenarios, branching allows you to split the logic in your flow, making it easier to build, understand, and manage. For example, a common scenario for syncing orders between two systems is to: &#x20;

**\[1]** Pull orders from the source system **\[2]** Check if the customer record exists in the destination system **\[3]** If not, create it **\[4]** Check if the address exists in the destination system **\[5]** If not, create it **\[6]** Decrement stock record in the destination system **\[7]** Send order info to the destination system

This can all be achieved using one long process flow; however, the `branch` shape provides the ability to organise the steps logically. So:

* Branch 1 creates/updates customer records
* Branch 2 creates/updates address records
* Branch 3 updates stock records
* Branch 4 sends the orders to a warehouse system

{% hint style="info" %}
A single incoming payload for [any process flow shape](https://open.gitbook.com/~site/site_dIV1g/~/revisions/9dTvdvRJIRVZQXuBMMnJ/process-flows/building-process-flows/process-flow-shapes) should not exceed 500MB.

We recommend processing multiple, smaller payloads rather than one single payload (1000 x 0.5MB payloads are more efficient than 1 x 500MB payload!).

For payloads up to 500MB, consider adding a[ flow control shape](https://open.gitbook.com/~site/site_dIV1g/~/revisions/9dTvdvRJIRVZQXuBMMnJ/process-flows/building-process-flows/process-flow-shapes/standard-shapes/flow-control-shape) to batch data into multiple, smaller payloads. Payloads exceeding 500MB should be batched at source.
{% endhint %}

## Need to know

* By default, the same data - i.e. any payloads that the `branch` shape receives - flow down every branch. For more information, see [How data flows through a branch shape](#how-data-flows-through-a-branch-shape).&#x20;
* All steps in a branch must be completed before the next branch starts.
* If one branch fails, any subsequent branches will not start.
* Nested `branch` shapes are permitted - so you can have a `branch` shape within a branch.
* If a branch includes a [run process flow](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/run-process-flow-shape) shape, the required sub-flow is triggered immediately, and subsequent branch steps continue. The `branch` shape does NOT wait for the sub-flow to complete.
* If a branch includes a [try/catch](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/try-catch-shape) shape, the catch steps are processed at the end of the flow, not at the end of the branch.&#x20;
* A `branch` shape should never include a [route](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/route-shape) shape. Instead, use [filter](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/filter-shape) shapes to achieve the same result - for example:\
  ![](https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F1MfLfHc3oMXaGcnZg9Ln%2Froute%20within%20branch%20-%20right%20and%20wrong.png?alt=media\&token=a5a37fb5-0d2b-41e3-8cee-b12e90e3e712)
* There is no limit on the number of branches that can be added for a `branch` shape.&#x20;

## How data flows through a branch shape

By default, any payload that a `branch` shape receives passes through each branch, sequentially. Consider the example below:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FO9ts8TQkGrp72wb0pxEu%2Fbranch%20shape%20example%201.png?alt=media&#x26;token=9d36387a-77ac-4a46-a556-2c6ffe3efb16" alt=""><figcaption></figcaption></figure>

Here, the `branch` shape receives five payloads from the preceding [flow control shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/flow-control-shape). First, these five payloads are processed by `branch 1`, then the *same* five payloads are processed by `branch 2`, followed by `branch 3`.&#x20;

However, what if we wanted to pass data from one branch to another? For example, suppose that `branch 1` includes a series of steps to prepare data for onward processing in branches 2, 3, and 4. For example:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FD3jfC4qyJdXkG1Welr7r%2Fbranch%20shape%20example%202.png?alt=media&#x26;token=f0758ce5-bad8-4930-981f-4683d778cf36" alt=""><figcaption></figcaption></figure>

In this case, we can add `branch 1` data to a [cache](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape), and then either:

* [Load cached data in any subsequent branches using a load from cache shape](#loading-branch-data-from-a-cache)
* [Reference cached data in any subsequent branches using a cache lookup transform](#referencing-branch-data-from-a-cache)

### Loading branch data from a cache

If you use the [add to cache](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) shape in a branch, the cached data can be loaded later in the same branch and/or in subsequent branches. Placing a [load from cache](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/load-from-cache-shape) shape at the very start of a branch results in the cached data being loaded into the branch and processed by subsequent shapes - i.e. this branch does NOT process the data received by the parent `branch` shape.&#x20;

In the example below, `branch 1` prepares and filters *five* incoming payloads. At the end of this branch,  *one* payload is filtered out, and *four* payloads are cached. At the start of `branch 2`, we load the cached data (*four* payloads) and these are processed by all subsequent shapes in this branch. Next, `branch 3` and `branch 4` do not load cached data, so these go on to process the original incoming payloads.

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F9Dl0w9yFiAd9TQuaGWI9%2Fbranch%20shape%20example%203a.png?alt=media&#x26;token=0e5cb25a-6a48-4349-9382-c5932fcb4c43" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For more information, please see the [add to cache shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) and [load from cache shape ](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/load-from-cache-shape)pages.&#x20;
{% endhint %}

### Referencing branch data from a cache

You can use the [cache lookup transform function](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations/available-transform-functions/other-transform-functions/cache-lookup-transform-function) to map field values from an existing cache. In this scenario, we aren't loading the cached data into the branch for onward processing - data coming through will be payloads that the parent `branch` shape received. This is shown in the example below:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F44Wj6e4uijJBMes2G3q5%2Fbranch%20shape%20example%204.png?alt=media&#x26;token=5d5b1976-bd4c-4952-a11e-c3c1dc780fb4" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For more information, please see [Referencing a cache in mapping transformations](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/referencing-a-cache-in-mapping-transformations) in our *cache* section.&#x20;
{% endhint %}

## Adding a branch shape&#x20;

{% stepper %}
{% step %}
**Add a branch shape**

In your process flow, add the `branch` shape in the usual way:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fsy0bwFV60xH1geZ64thR%2Fbranch%20shape%20in%20palette.png?alt=media&#x26;token=cae05748-18a6-4d54-8f56-4e5e2e6e06cc" alt="" width="375"><figcaption></figcaption></figure></div>

The shape is added to your flow with two path stubs - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FyMohl9Y6dLDUxSKjbUTI%2Fbranch%20shape%20added.png?alt=media&#x26;token=0315f975-6ee4-4581-af80-d5f0ae8fdd9d" alt="" width="314"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Configure branches**

Click the `settings` icon for the `branch` shape:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FOlbn1hrPHIFs0n1Lrjhx%2Fbranch%20shape%20config%201.png?alt=media&#x26;token=ba25ebe0-a35c-4195-bd81-2952ac4ac341" alt="" width="322"><figcaption></figcaption></figure></div>

Branch names are shown for the two placeholders:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FbhmuL9OOV9PaaPT5IPPN%2Fbranch%20shape%20config%202.png?alt=media&#x26;token=eea05157-7bed-4be1-94b4-461aa8889459" alt="" width="375"><figcaption></figcaption></figure></div>

...you can change these names as appropriate - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F1Gip8RZck6StzUBRnZiP%2Fbranch%20shape%20config%203.png?alt=media&#x26;token=0b560a30-2438-4c21-ac5e-2ab32d8f8a6f" alt="" width="375"><figcaption></figcaption></figure></div>

...and add more branches if needed:&#x20;

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FlUw6Bp7PQ7CTKTeYhIKc%2Fbranch%20shape%20config%204.png?alt=media&#x26;token=15f5bb90-a1af-4179-bec8-3724093ead2f" alt="" width="375"><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Having added new branches, the `branch` shape must be saved before they can be re-sequenced. Save the shape, and then return to settings to resequence the new branches.
{% endhint %}
{% endstep %}

{% step %}
**Save changes**

Once all branches have been added, save changes:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FHyYlK5DHmkLsLmSt7juH%2Fbranch%20shape%20config%205.png?alt=media&#x26;token=781ee7af-3dd8-4f55-92f1-10fcf9c1159f" alt="" width="375"><figcaption></figcaption></figure></div>

The `branch` shape is updated, and you'll see a placeholder stub for each branch you added in the previous step:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FNMXPsbxSx6n1PjuU97jx%2Fbranch%20shape%20config%207.png?alt=media&#x26;token=9054a0a4-eb77-4c74-b290-5c2f41f08556" alt="" width="334"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Build branches**

Now you can add shapes to each branch in the normal way. As soon as you add your first shape to a branch, empty shapes are added to the others - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fzh08Ye2s9CxLPSu9dcB7%2Fbranch%20shape%20config%208.png?alt=media&#x26;token=6fd16d4a-4a3f-4cbb-9387-3fa11d01d3b3" alt=""><figcaption></figcaption></figure></div>

Click on these placeholders to replace them with the required shape from the shapes palette:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FwEyY54xfMMMBkHoH6REF%2Fbranch%20shape%20config%209.png?alt=media&#x26;token=ae45c2df-e380-4bb2-8cdb-00c3e99f98a2" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Nested branch shapes are allowed - so if required, you can have a `branch` shape within a branch:

<img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fy2JLbHBQcfjR7Z405BkZ%2Fbranch%20shape%20config%2010.png?alt=media&#x26;token=508a8efc-1ece-4c0c-af6d-e3902a1a6bbe" alt="" data-size="original">

There are no limitations on the number of nested levels; however, nesting too deeply will have an impact on flow clarity.&#x20;
{% endhint %}
{% endstep %}
{% endstepper %}

## Managing branches

Having added branches to a branch shape, you may wish to:

* [Rename a branch](#renaming-a-branch)
* [Re-sequence a branch](#re-sequencing-branches)
* [Delete a branch](#deleting-a-branch)

These tasks are completed from `branch` shape settings:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7Bj9txBzO0nwId0ZRnOq%2Fre-sequence%201.png?alt=media&#x26;token=9428cba7-0928-42ac-9ddd-fc34dc2c80ec" alt="" width="320"><figcaption></figcaption></figure></div>

### Renaming a branch

You can rename a branch at any time - simply overwrite the existing name and save shape settings:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FyeEBdtkAwU9sAOFg88M7%2Frename%201.png?alt=media&#x26;token=fe9d6403-d69a-41d6-a53f-892762314508" alt="" width="375"><figcaption></figcaption></figure></div>

### Re-sequencing branches

Before re-sequencing branches, ensure that any new branches have been saved (save the shape and re-access shape settings). Then it's just a matter of using up/down arrows to move branches to the right place: &#x20;

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FPmnEapkRXNj099WqbsXw%2Fre-sequence%202.png?alt=media&#x26;token=98396c72-cb02-4abe-93f6-f14639b6d49d" alt="" width="375"><figcaption></figcaption></figure></div>

### Deleting a branch

When a branch is deleted, it's removed entirely - any shapes currently defined in its path are also removed. To delete a branch, click the associated delete button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fys7UFtRFaNlCtQNUtqwN%2Fdelete%20branch%202.png?alt=media&#x26;token=d8159276-aab6-4c2c-a0fb-056377a41249" alt="" width="375"><figcaption></figcaption></figure></div>

## Managing branch paths

Shapes are added to branch paths in the normal way - add shapes from the shapes palette and configure them as needed.

{% hint style="warning" %}
If you remove the only shape in a branch path, the empty branch 'stub' remains in place, so sequencing doesn't change. If you don't need the branch anymore, access `branch` shape settings and [remove](#deleting-a-branch) it.&#x20;
{% endhint %}
