# Referencing flow variables in custom scripts

## Introduction

If required, you can reference (and therefore manipulate) flow variables in custom scripts. The possibilities here are only as limited as your development expertise however, a simple example might be where you want to generate a running count of order lines, to be output to a `total` field in a target system.

To achieve this, you would:

1. [Add a flow variable](/product-documentation/process-flows/building-process-flows/dynamic-variables/flow-variables/referencing-flow-variables-in-a-process-flow.md#stage-1-define-flow-variables-that-can-be-used) named `running_total` to your process flow settings.
2. [Write a custom script](/product-documentation/developer-hub/custom-scripting.md) which loops over each received order line and updates the `running_total` variable as it goes.
3. Add the custom script to your process flow via a [script shape](/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/script-shape.md).
4. Add a [map shape](/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape.md) to your process flow and include a rule which maps a custom string transform for `{{flow.variables.running_total}}`, to the `total` field in the target system.

## Flow variable syntax for custom scripts

To reference flow variables in a custom script, the required syntax is as follows:

{% code title="PHP 8.1" lineNumbers="true" %}

```javascript
['flow']['variables']['variableName']
```

{% endcode %}

{% code title="Javascript Node 18" lineNumbers="true" %}

```javascript
flow.variables.variableName
```

{% endcode %}

{% code title="C# 8" lineNumbers="true" %}

```javascript
["flow"]["variables"]["variableName"]
```

{% endcode %}

{% code title="Python 3" lineNumbers="true" %}

```javascript
['flow']['variables']['variableName']
```

{% endcode %}

{% code title="Ruby 3" lineNumbers="true" %}

```javascript
['flow']['variables']['variableName']
```

{% endcode %}

In all cases, the `variableName` element should be replaced with the actual flow variable name. For example:

{% code title="PHP 8.1" lineNumbers="true" %}

```php
['flow']['variables']['running_total']
```

{% endcode %}

## Example

The example script below takes a **flow variable** named `customerID` and sets the value to `1234567`:

{% code title="PHP 8.1" lineNumbers="true" %}

```php
<?php

/**
 * Handler function.
 *
 * @param array $data [
 *      'payload'   => (string|null) the payload as a string|null
 *      'variables' => (array[string]string) any variables as key/value
 *      'meta'      => (array[string]string) any meta as key/value
 *    ]
 */

function handle($data)
{
    $data['flow']['variables']['customerID'] = 1234567;

    return $data;
}
```

{% endcode %}

So, wherever the `customerID` flow variable is referenced in a process flow, its value would be set to 1234567 when the process flow runs.

{% hint style="info" %}
When you update flow variables via a script, those updates persist for the duration of the flow run. Once the process flow has completed, default values are restored.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/dynamic-variables/flow-variables/referencing-flow-variables-in-custom-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
