JS Code
Overview
JS Code allows you to execute Javascript code inside your flow.
It is necessary to use return statement to return value, returned value is assigned to property defined in targetPath.
It is possible to access doc node variables inside JS Code step, however, here double curly braces are NOT used. It is not possible to modify doc nodes from step within.
Workspace and Environment variables can be accessed under $env field. Headers are also available under $headers.
Configuration
Code block (codeBlock)- javascript code block, the return value will be assigned to thedocnodeTarget path (targetPath)- thedocnode on which to store the return value
Step execution configuration
It is possible to configure step behaviour in runtime through environment variables:
DISABLE_CODE_IVM_STEP
If true - disables JS Code step globally
Examples
to access doc nodes that have dashes in their names use the following syntax: globalThis['node-with-dashes']
Calculate sum of two numbers
Let's say we want to calculate the total amount for an order - we can do that using multiplication in JS Code step
Input
{
"price": 120,
"amount": 2
}
Step configuration
Code block (codeBlock)- the Javascript code to be executed:return price * amountTarget path (targetPath)-docnode on which to assign the result:sum
Example flow
tags: []
steps:
- stepType: code-ivm
config:
codeBlock: return price * amount
targetPath: sum
name: JS Code
description: ""
condition: ""
sampleData:
price: 120
amount: 2
Result
{
"doc": {
"price": 120,
"amount": 2,
"sum": 240
},
"errors": [],
"performance": {
"steps": [
{
"step": "code-ivm",
"executionTime": 13
}
],
"executionTimeOfFlow": 14,
"timeMetric": "ms"
}
}
Using environment variables
Let's say we want to access environment variable that stores custom URL base and make a full address inside JS Code step.
Input
{
"query": "11",
"urlEnd": "/policy/?v="
}
Prerequisites
- In environment variables,
URL_BASEvariable is set tohttps://example.com.
Step configuration
Code block (codeBlock)- the Javascript code to be executed:const adr = $env.URL_BASE + urlEnd + query; return adr;Target path (targetPath)-docnode on which to assign the result:result
Result
{
"doc": {
"query": "11",
"urlEnd": "/policy/?v=",
"result": "https://example.com/policy/?v=11"
},
"errors": []
}
Example flow
tags: []
steps:
- stepType: code-ivm
config:
codeBlock: const adr = $env.URL_BASE + urlEnd + query; return adr;
targetPath: result
name: JS Code
description: ""
condition: ""