JS Code
Overview
JS Code allows you to execute Javascript code inside your flow.
The return value of the code block will be set to the 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
codeBlock- javascript code block the return value will be assigned to thedocnode.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
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
codeBlock- the Javascript code to be executed:return price * amounttargetPath-docnode on which to assign the result:sum
Example flow
{
"name": "exampleJsCode",
"flow": {
"name": "exampleJsCode",
"description": "",
"steps": [
{
"stepType": "code-ivm",
"color": "rgb(223,204,241)",
"displayName": "JS Code",
"isSelected": true,
"config": {
"codeBlock": "return price * amount",
"targetPath": "sum"
}
}
]
},
"inputSchema": {
"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
codeBlock- the Javascript code to be executed:const adr = $env.URL_BASE + urlEnd + query; return adr;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
{
"name": "exampleJsCode",
"flow": {
"name": "exampleJsCode",
"description": "",
"steps": [
{
"stepType": "code-ivm",
"color": "rgb(223,204,241)",
"displayName": "JS Code",
"isSelected": true,
"config": {
"codeBlock": "const adr = $env.URL_BASE + urlEnd + query; return adr;",
"targetPath": "result"
}
}
]
}
}