Skip to main content
Version: ACE 5

JS Code Functions (deprecated)

Overview

JS Code allows you to execute Javascript code inside your flow.

The return value of the code block will be set to the target path

note

It is possible to access doc node variables inside JS Code step, however, here double curly braces are NOT used

note

Workspace and Environment variables can be accessed under $env field

Configuration

  • code block - javascript code block the return value will be assigned to the doc node
  • target path - the doc 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 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 * amount
  • Target Path - doc node on which to assign the result: sum

Result

{
"doc": {
"price": 120,
"amount": 2,
"sum": 240
},
"errors": []
}

Example flow

Sample JS Code 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 - doc node 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_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

Sample JS Code 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