JS Code Functions (deprecated)
Overview
This step is deprecated and will be removed in ACE 26.x
It is considered less safe than JS Code step and should be avoided in new flows.
JS Code allows you to execute Javascript code inside your flow.
Last statement result is assigned to property defined in targetPath.
Global scope variables in script are assigned back to doc object.
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 thedocnodetarget path- thedocnode 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 runtime 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 * amountTarget Path-docnode on which to assign the result:sum
Result
{
"doc": {
"price": 120,
"amount": 2,
"sum": 240
},
"errors": []
}
Example flow
tags: []
steps:
- stepType: code
config:
codeBlock: price * amount
targetPath: sum
name: JS Code Functions
description: ""
condition: ""
sampleData:
price: 120
amount: 2
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-docnode on which to assign the result:result
Result
{
"doc": {
"result": "complexAsyncProcessingResult"
},
"errors": []
}
Example flow
tags: []
steps:
- stepType: code
config:
codeBlock: const fn = async () => 'complexAsyncProcessingResult'; fn();
targetPath: result
name: JS Code Functions
description: ""
condition: ""
sampleData: {}
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_PATHvariable 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-docnode on which to assign the result:fullConfigPath
Result
{
"doc": {
"configName": "myConfig.json",
"fullConfigPath": "C:\\Users\\user\\Documents\\workspace\\myConfig.json"
},
"errors": []
}
Example flow
tags: []
steps:
- stepType: code
config:
codeBlock: const path = require('path'); path.join($env.WORKSPACE_PATH, configName)
targetPath: fullConfigPath
name: JS Code Functions
description: ""
condition: ""
sampleData:
configName: myConfig.json