Workflow variables
Workflow variables are a mechanism to use dynamic and calculated values as workflow node inputs. They are available in
Torq version 2.0. If you have a need to use a same value for multiple inputs, that you might want to change, you can use
a variable to change it only in one location instead of editing all the inputs separately.
Creating a workflow variable
In a workflow, go to configurations and select either Add Workflow Variable or Add System Variable. A workflow variable
is only usable in that specific workflow, but system variable is usable across all workflows (and in the future possibly
even outside workflows). In this article we call both simply as "workflow variable" as their behavior is exactly the
same.
Add a name for the variable, select a datatype and add the definition.
The most basic variable definition is just a constant. Note that text has to be added in quotes.
Beside constants you can define the variable as a formula (with math operations), or use the "template-variables" inside
the variable, to make it dynamic based on data available when the workflow is running. This is explained better at the
end of the article.
Using a workflow variable
After a workflow variable is created it can be dragged to some input fields in workflow nodes. Note that each input
accepts only one type of variable. An exception is the text area inputs for the Notification node and API Client node
POST request body. To those text areas you can drag any workflow variable, and it will be added to the message as a
template variable. A little icon in an input tells if a workflow variable is usable for that field. The icon also tells
the type of variable that is accepted for that field. If there is no icon (except for the mentioned text areas), it
means that workflow variables are currently not usable for it.
Above is presented the three different types of inputs that accept variables: text, number and datetime. Below, a
workflow variable is added to an input.
Testing workflow variable behavior
A simple way to test if a variable behaves as expected is to use it in a Notification node (with or without defining any
communication methods). Choose the "Each channel" option for the node. Drag the workflow variable into the notification
message (it should appear as a template variable), and run Dry-Run. You will see the evaluated variable value in the
Workflow Logs.
Below there was a variable that was defined as 2 * 100000. The evaluated value is seen in the messages.
Overwriting workflow variables when triggering workflow via gRPC
It's now possible to trigger workflows via gRPC API (refer to the article about gRPC API ). When triggering a workflow
via the API, you can overwrite variables defined for the workflow by setting them to the gRPC call parameters. Be sure
to use exactly the same name as the WorkflowVariable.name parameter as the variable name in Torq.
Creating advanced variable definitions
Let's get back to defining the workflow variables. As stated mathematical operations as well as dynamic "template
variables" can be used in the workflow variable definition. Choose the template variables from the dropdowns above the
variable definition. The variable is evaluated with https://github.com/Knetic/govaluate . Refer to their docs to see
what is possible. Basically all basic math operations (+ - / * & | ^ ** % >> <<) and string concatenation with +
operator are possible. Always remember to choose the correct data type that you aim to evaluate!
Some examples:
- Numeric variable: "Half the channel capacity": {{Channels.capacity}}/2
- Text variable (note the quotes!): "{{Channels.shortChannelId}} - {{Channels.gauge}}"