# Referencing flow & meta variables in mapping transformations

## the Introduction

You can reference flow variables in [field mapping transformation](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations)s, using a `custom string` transformation in the [map shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape). &#x20;

Most typically, the `custom string` transformation type is used to specify some custom text to be applied in a target field. For example, you might choose to add a particular sales rep's name to a `reference` field for all orders being synced.&#x20;

However, if your process flow is using **flow variables**, you can reference these in custom string transformations, instead of static text. When the process flow runs, the flow variable value is mapped to your given target field. If you later decide to update flow variables (either manually or via a [custom script](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/dynamic-variables/flow-variables/referencing-flow-variables-in-custom-scripts)), those updates are mapped next time the process flow runs.&#x20;

The steps required to achieve this can be summarised in two stages:

* [Stage 1: Define flow variables that can be referenced](#stage-1-define-flow-variables-that-can-be-used)
* [Stage 2: Add references to defined flow variables](#stage-2-add-references-to-defined-flow-variables)&#x20;

## Stage 1: Define flow variables that can be used

Any flow variables that you want to reference in field transformations should be added as variables within the **process flow settings**. To do this, follow the steps below.

**Step 1**\
Access the process flow that you want to update and [make sure that you've switched to the required version](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-versioning).

{% hint style="info" %}
Typically, you would update the **draft** version, and then deploy changes when you are ready.
{% endhint %}

**Step 2**\
Select settings (the cog icon) from the actions bar:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F955YqunISPFCHlDrJD7p%2Fflow%20variables%201.jpg?alt=media&#x26;token=4315895f-a1a0-4b39-a6ae-084460e49913" alt="" width="306"><figcaption></figcaption></figure></div>

**Step 3**\
Look for the **variables** section in the **flow settings** panel - 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%2FIzp2IkErsRAaQ28kha0e%2Fcs0a.jpg?alt=media&#x26;token=9f30c8ee-aa86-4ca0-a3dc-8f8828a96c9b" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 4**\
Click the **add new variable** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fmj7M8HybGkJgqr2WNsNW%2Fcs0b.jpg?alt=media&#x26;token=305f36d4-efea-4154-ae56-932513be1862" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 5**\
In the **name** field, enter the name (i.e. the API parameter name) of this variable. For our example, the variable is named `rep`:&#x20;

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FAYKAWFWPy4ouBqsCyuJH%2Fcs1.jpg?alt=media&#x26;token=ec17eaa1-0e63-4925-925e-66dc93adca04" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 6**\
Click in the **select a type** field and select the data type for this variable:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FXmyz9bpGjQbQZhFQE3T0%2Fcs2.jpg?alt=media&#x26;token=126b1f96-ab7b-4275-aaa3-7adc079fdd69" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 7**\
Enter the required value to be used wherever this variable is found in the process flow - 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%2FG5iHimSg6MXT0VUbA8WI%2Fcs3.jpg?alt=media&#x26;token=fcf0d813-35e9-44cf-b0fa-979b3f761ac0" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 8**\
Save changes.

### Stage 2: Add references to defined flow variables &#x20;

Having defined your required **flow variables**, they can be referenced in `custom string` field mapping transformations. Typically, this will be for scenarios where you want to map the output from a flow variable into a target field.&#x20;

{% hint style="info" %}
If you've added/updated a [map shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape) before, you'll be used to selecting a **source** field and a **target** field. However, when we map a **flow variable** value to a target field, we don't select a **source** field - the custom string transformation is our data source.
{% endhint %}

**Step 1**\
In your process flow, access settings for the **map shape** that you want to update with a flow variable:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FvozeX9x4sHlRa8okUFCk%2Fmap%20transform%201.jpg?alt=media&#x26;token=63b29e3e-b863-47ad-9313-459169f3b1ba" alt="" width="358"><figcaption></figcaption></figure></div>

**Step 2**\
Click the **add mapping rule** option - 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%2F3d69bjSqbSvrdDT8H0W6%2Fmap%20transform%202.jpg?alt=media&#x26;token=a3b8eb88-8bf7-42a0-934c-a4aeb4204661" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 3**\
Click the **add transform** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FxOOae75HpSijTH2Ou8AJ%2Fmap%20transform%203.jpg?alt=media&#x26;token=9f624f78-9604-47d4-b599-86d75760b7ce" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 4**\
Click the **add transform** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F3Ua8TKaE4TZFejZDXzLl%2Fmap%20transform%204.jpg?alt=media&#x26;token=9ad403ed-d029-4c81-ab5c-ee9839b6316d" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 5**\
Click in the **name** field to access a list of all available transform functions, then select **custom string**:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FYQ8TdDHNMWeZoFR1gZfu%2Fmap%20transform%205.jpg?alt=media&#x26;token=1a241fd3-5b8c-4e06-b3d8-72b9fd0306ab" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 6**\
Move down to the **custom string** field and enter the following value:

{% code lineNumbers="true" %}

```
{{flow.variables.variable}}
```

{% endcode %}

...where the `variable` element should be replaced with the name of the **flow variable** defined in process flow settings ([stage 1](#stage-1-define-flow-variables-that-can-be-used)) that you want to reference. Using our example, this would be:

```
{{flow.variables.rep}}
```

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%2FayQqCh0Uw3l8WLs4Cqew%2Fmap%20transform%206.jpg?alt=media&#x26;token=2853b047-07a5-4396-aa0f-d21c056fac3f" alt="" width="375"><figcaption></figcaption></figure></div>

You can also reference [metadata variables](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/payload-variables#variables-and-parameters-from-payload-metadata) in the same way, using the meta syntax:

`[[meta.unique_key]]`<br>

**Step 7**\
Accept your changes:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F6lK526cqzn67sejLGOlS%2Fmap%20transform%207.jpg?alt=media&#x26;token=e0b8109e-ecef-418a-a46a-24e72a9ab2e8" alt="" width="375"><figcaption></figcaption></figure></div>

...then save the transformation:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FFtS8qnNG4oYhV1SeWxcd%2Fmap%20transform%208.jpg?alt=media&#x26;token=bf2fae02-0a40-44f7-aeee-7cefd1732959" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 8**\
Now you can select a target field in the usual way - 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%2F1hG7URex0oKXVNdfuKUc%2Fmap%20transform%209.jpg?alt=media&#x26;token=9819a58e-8213-458f-ac2a-ed981ecc58cd" alt="" width="375"><figcaption></figcaption></figure></div>

...then:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FzWNexMmXEFYjso1QMYn7%2Fmap%20transform%2010.jpg?alt=media&#x26;token=644c0ddc-ec97-4745-8896-e12cd72f2098" alt="" width="375"><figcaption></figcaption></figure></div>

...then:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FwXpTWKTeHfRQPk2FFmgi%2Fmap%20transform%2011.jpg?alt=media&#x26;token=ff6f1e13-5428-457b-8b96-eaa70d4bea1a" alt="" width="375"><figcaption></figcaption></figure></div>

**Step 9**\
Once your mapping is complete, the row should be displayed without a source field - 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%2FRQf6U2KUWJF5TGf2100h%2Fmap%20transform%2012.jpg?alt=media&#x26;token=74e73b18-d889-4249-82fe-cf447d99bf5d" alt="" width="375"><figcaption></figcaption></figure></div>

From here you can save changes or add more mapping rules as needed. Next time the process flow runs, the value of this flow variable will be mapped to the target field.
