Run JavaScript action for Workflows

The Run JavaScript action for Workflows enables you to build a custom task running JavaScript code in a workflow.

The Dynatrace Platform provides a JavaScript runtime for custom scripts in Workflows.

Custom JavaScript tasks in workflows are provided with an executionId, actionExecutionId, and loopItemValue that, in conjunction with the automation SDKs, give you access to task results, workflow execution, and loop item parameters, directly as JavaScript objects without using Jinja expressions.

To use the automation SDKs, import them into the custom script task. Then, they can be initialized with the execution context.

We offer both a client automation SDK, which gives full access to the Automation API, and a convenience-focused automation utils SDK.

Executing the Run JavaScript for Workflows action is similar to running the code in the Function executor. You can find the results in the Result tab of the Execution that you could use in subsequent tasks.

Run JavaScript action security

  • The Run JavaScript action does not support expressions in its input to avoid the possibility of code injection.
  • All HTTP calls are validated against the global allowlist.
  • If you import third-party libraries for your JavaScript action, the allowlisted CDN domains provide access to the entire package portfolio. Dynatrace JavaScript runtime is robust against certain attack vectors, but you might accidentally allow malicious code. Make sure to mirror dependencies that you rely on in your internal infrastructure and monitor their security implications with Dynatrace Application Security or third-party tools like Snyk.

We strictly advise against returning any secret as part of the result. Every result is accessible in the executions to anyone with read access to the workflow.

Run JavaScript action requirements

  • See JavaScript runtime for Node and Web API compatibility of the Dynatrace JavaScript runtime.
  • The JavaScript runtime times out after 120 seconds. Thus any Run JavaScript action will also timeout after 120 seconds the latest. Please mind that this timeout is not extendable by setting a higher timeout value in task options.
  • The JavaScript runtime provides up to 256 MB RAM.
  • The JavaScript runtime and thus Run Javascript actions can't return a binary result. A workaround would be to serialize the payload into an object.
  • The script of a Run JavaScript action size is limited to about 5 MB. Additional context information implicitly sent by the AutomationEngine on action invocation, for exmaple, workflow and action execution identifier, also account for this limit.
  • The result of Run JavaScript action is limited to 1 MB.
  • There is a limit on concurrent requests to the underlying infrastructure (Function executor) that executes the JavaScript runtime. In case your Run JavaScript tasks run into such issue, you can use the task options retry functionality to mitigate the problem.

Task result

Your JavaScript actions can retrieve the result of a previous task and use it for further processing.

Example using the automation-utils SDK to access the result

// import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
export default async function ({ executionId }) {
// load the execution object using the current executionId
var ex = await execution(executionId)
// get the result of task 'my_task'. 'my_task' must be a predecessor.
var myResult = await ex.result('my_task');
// log the result object
console.log('The whole result object: ', myResult);
console.log('only one variable: ', myResult.myVariable)
}

Example using the client-automation SDK to access the result

// import of sdk modules
import { executionsClient } from '@dynatrace-sdk/client-automation';
export default async function ({ executionId }) {
// load the execution object using the current executionId
var config = {executionId, id: 'my_task'}
var myResult = await executionsClient.getTaskExecutionResult(config)
// log the result object
console.log('My task result: ', myResult)
console.log('only one variable: ', myResult.myVariable)
}

Available execution context

The following execution context is available out of the box and can be accessed for any use case that demand for it.

export default async function ({ executionId, actionExecutionId }) {
//log available execution context ids
console.log('Workflow execution id: ', executionId);
console.log('Action execution id: ', actionExecutionId)
}

The task execution id is currently not available. For loop item context, please see the Task loop section below.

Task loop

When using the option to loop a task, you might want to access the value of the current loop item. You can use the loopItemValue parameter to access the value of the item for the current iteration.

Example using loopItemValue

export default async function ({ loopItemValue }) {
// log the current value of the loop item
console.log(loopItemValue)
}

Import third-party libraries

If you need a certain functionality in your JavaScript action that is provided by third-party libraries, you can load the library via a URL import.

Restrictions apply:

Example - use XMLJSON library to parse XML input

Let's say your backend produces legacy XML output, but you need to process data as JSON. In such a case, you can let the a generic XML2JSON Library to parse the content rather than writing your own code do it.

  1. Add the XMLJSON librabry URL to allowed outbound connections.

    1. Go to Settings > Preferences > Limit outbound connections.
    2. Select Add item and add esm.sh the allow-list.
  2. Add a snippet like below to your JavaScript action to parse the XML, convert it to JSON and use as input for your action.

// Load the XML parser from ESM
import xml2js from "https://esm.sh/xml2js@0.6.2";
export default async function() {
// Dummy XML, can be fetched from your back-end
const xml = "<root><list><item>Hello</item><item>World</item></list></root>";
const parser = new xml2js.Parser();
const json = await parser.parseStringPromise(xml);
return json;
}

Package CDNs like esm.sh, unpkg, JSR, JSDELIVER, or Deno offer compatible packages.

Note that some of those libraries either depend on Node.js internals or Deno internals, which the Dynatrace JavaScript runtime does not provide. See JavaScript runtime for Node and Web API compatibility of the Dynatrace JavaScript runtime.