JS Code Functions (deprecated)
Overview
JS Code Functions
allows you to execute Javascript code inside your flow.
The return value of the code block will be set to the target path
It is possible to access doc node variables inside JS Code step, however, here double curly braces are NOT used
Workspace and Environment variables can be accessed under $env
field
Configuration
code block
- javascript code block the return value will be assigned to thedoc
nodetarget path
- thedoc
node on which to store the return value
Step execution configuration
It is possible to configure step behaviour in runtime through environment variables.
ACE_CODE_ENABLE_REQUIRE
If true
- enables use of node require inside code step
Available modules for require
:
- Node.js core modules
- ACE runtime dependencies (not guaranteed, can change without warning!)
- Globally installed npm modules
It is possible to add new modules by extending ACE flow runner image and installing npm modules globally.
DISABLE_JS_CODE_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
Code block
- the Javascript code to be executed:price * amount
Target Path
-doc
node on which to assign the result:sum
Result
{
"doc": {
"price": 120,
"amount": 2,
"sum": 240
},
"errors": []
}
Example flow
{
"name": "exampleJsCode",
"flow": {
"name": "exampleJsCode",
"description": "",
"steps": [
{
"stepType": "code",
"color": "rgb(223,204,241)",
"displayName": "JS Code Functions",
"isSelected": true,
"config": {
"codeBlock": "price * amount",
"targetPath": "sum"
}
}
]
},
"inputSchema": {
"price": 120,
"amount": 2
},
"createDate": "2021-10-08T16:55:48.092Z",
"id": 1633712148189
}
Use async functions
Let's say we want to execute a complex async function - we can do that by defining async function and calling it directly in JS Code
step
Input
{}
Step configuration
Code block
- the Javascript code to be executed:const fn = async () => 'complexAsyncProcessingResult'; fn();
Target Path
-doc
node on which to assign the result:result
Result
{
"doc": {
"result": "complexAsyncProcessingResult"
},
"errors": []
}
Example flow
{
"name": "exampleJsCode",
"flow": {
"name": "exampleJsCode",
"description": "",
"steps": [
{
"stepType": "code",
"color": "rgb(223,204,241)",
"displayName": "JS Code Functions",
"isSelected": true,
"config": {
"codeBlock": "const fn = async () => 'complexAsyncProcessingResult'; fn();",
"targetPath": "result"
}
}
]
},
"inputSchema": {},
"createDate": "2021-10-08T16:55:48.092Z",
"id": 1633712148188
}
Use workspace variables and node's path module
Let's say we want to access a workspace variable that stores the config path inside JS Code
step and utilize node's path module to calculate a full path to the file.
Input
{
"configName": "myConfig.json"
}
Prerequisites
- In workspace variables,
WORKSPACE_PATH
variable is set to the path of the workspace"C:\\Users\\user\\Documents\\workspace"
. - In environment variables,
ACE_CODE_ENABLE_REQUIRE="true"
is set.
Step configuration
Code block
- the Javascript code to be executed:const path = require('path'); path.join($env.WORKSPACE_PATH, configName)
Target Path
-doc
node on which to assign the result:fullConfigPath
Result
{
"doc": {
"configName": "myConfig.json",
"fullConfigPath": "C:\\Users\\user\\Documents\\workspace\\myConfig.json"
},
"errors": []
}
Example flow
{
"name": "exampleJsCode",
"flow": {
"name": "exampleJsCode",
"description": "",
"steps": [
{
"stepType": "code",
"color": "rgb(223,204,241)",
"displayName": "JS Code Functions",
"isSelected": true,
"config": {
"codeBlock": "const path = require('path'); path.join($env.WORKSPACE_PATH, configName)",
"targetPath": "fullConfigPath"
}
}
]
},
"inputSchema": {
"configName": "myConfig.json"
},
"createDate": "2021-10-08T16:55:48.092Z",
"id": 1633712148187
}