To view this content in our official product documentation, click here.
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:
-
Add a flow variable named
running_totalto your process flow settings. -
Write a custom script which loops over each received order line and updates the
running_totalvariable as it goes. - Add the custom script to your process flow via a script shape.
- Add a map shape to your process flow and include a rule which maps a custom string transform for , to the
totalfield 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" %}
['flow']['variables']['variableName']{% code title="Javascript Node 18" lineNumbers="true" %}
flow.variables.variableName{% code title="C# 8" lineNumbers="true" %}
["flow"]["variables"]["variableName"]{% code title="Python 3" lineNumbers="true" %}
['flow']['variables']['variableName']{% code title="Ruby 3" lineNumbers="true" %}
['flow']['variables']['variableName']In all cases, the variableName element should be replaced with the actual flow variable name. For example:
{% code title="PHP 8.1" lineNumbers="true" %}
['flow']['variables']['running_total']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
/**
* 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;
}So, wherever the customerID flow variable is referenced in a process flow, its value would be set to 1234567 when the process flow runs.
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.
Comments
0 comments
Please sign in to leave a comment.