Skip to main content
Version: ACE 5

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

note

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.

note

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 the doc node.
  • targetPath - 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:

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 * amount
  • targetPath - doc node on which to assign the result: sum

Example flow

Sample JS Code 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_BASE variable is set to https://example.com.

Step configuration

  • codeBlock - the Javascript code to be executed: const adr = $env.URL_BASE + urlEnd + query; return adr;
  • targetPath - doc node on which to assign the result: result

Result

{
"doc": {
"query": "11",
"urlEnd": "/policy/?v=",
"result": "https://example.com/policy/?v=11"
},
"errors": []
}

Example flow

Sample JS Code flow
    tags: []
steps:
- stepType: code-ivm
config:
codeBlock: const adr = $env.URL_BASE + urlEnd + query; return adr;
targetPath: result
name: JS Code
description: ""
condition: ""