Run code
Step
run-codecontrolside effectsRuns a short JavaScript you write on the data so far: reshape it, calculate, or call one of your API connections. Runs in an isolated sandbox.
At a glance
| Property | Value |
|---|---|
| Step name | run-code |
| Resource | ClusterStep (lumen.ai/v1alpha1) |
| Palette group, category | control, logic |
| Credits | 1 credit each time it succeeds |
| Side effects | yes It changes something outside Lumen, so a retry asks first and a test shows what it would do instead of doing it. |
| Installation must provide | nothing |
Inputs
| Property | Value |
|---|---|
| Needs | nothing |
| Produces | fields |
| Works on | item one document at a time |
Needs and Produces are data types (file, text, fields, email). The editor offers a step only where what it needs is already on the path. See Documents and data.
Outputs
A later node references an output as {nodeId.field} in text, or nodeId.field in a condition.
| Output | Type |
|---|---|
result | string |
More outputs are declared in the setting outputs.
Settings
Set under config: on the flow node. Text settings may contain {nodeId.field} placeholders.
| Setting | Type | Default | Description |
|---|---|---|---|
coderequired | string | - | Code is the body of an async JavaScript function of (input, lumen, console); what it returns is the result.minLength 1; maxLength 65536 |
inputs | map of string | - | Inputs are the input's properties: a name, and text with {<nodeId>.<field>} placeholders. A value that is just one placeholder keeps the field's type. None passes every field so far. |
outputs | list of object | - | Outputs are the fields later steps can pick.maxItems 50 |
outputs[].namerequired | string | - | NameminLength 1; maxLength 64; pattern `^[A-Za-z][A-Za-z0-9_]*$` |
outputs[].typerequired | string | - | Typeone of string, number, boolean, date, money |
connections | list of string | - | Connections are the HTTP connections lumen.fetch may call.maxItems 10 |
methods | list of string | - | Methods are the methods lumen.fetch may use; none is GET and HEAD. |
timeoutSeconds | integer | - | TimeoutSeconds caps one run; zero is 5.minimum 1; maximum 30 |
The full JSON Schema of these settings: run-code.schema.json.
Example
Sum the lines of an invoice
config
code: |-
const lines = JSON.parse(input.lines)
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
console.log('lines', lines.length)
return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
lines: '{read.lines}'
outputs:
- name: total
type: number
- name: withVat
type: number
Use it in a flow
A node that runs run-code goes under spec.nodes of a Flow. Give it any id, and list the next node ids in next.
flow.yaml
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
name: run-code-example
namespace: tenant-acme
spec:
displayName: "Run code"
triggers:
- id: start
ref: {name: manual-run}
next: [run-code]
nodes:
- id: run-code
stepRef: {name: run-code}
config:
code: |-
const lines = JSON.parse(input.lines)
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
console.log('lines', lines.length)
return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
lines: '{read.lines}'
outputs:
- name: total
type: number
- name: withVat
type: number
Was this page helpful?