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 execution_id
and action_execution_id
that, in conjunction with the automation SDKs, give you access to task results, workflow execution 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.
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 modulesimport { execution } from '@dynatrace-sdk/automation-utils';export default async function ({ execution_id }) {// load the execution object using the current execution_idvar ex = await execution(execution_id)// get the result of task 'my_task'. 'my_task' must be a predecessor.var myResult = await ex.result('my_task');// log the result objectconsole.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 modulesimport { executionsClient } from '@dynatrace-sdk/client-automation';export default async function ({ execution_id }) {// load the execution object using the current execution_idvar config = {executionId: execution_id, id: 'my_task'}var myResult = await executionsClient.getTaskExecutionResult(config)// log the result objectconsole.log('My task result: ', myResult)console.log('only one variable: ', myResult.myVariable)}
Task loop
When using the option to loop a task, you might want to access the value of the current loop item. For this, the action_execution_id
is needed.
Example using the automation-utils SDK to access loop item
// import of sdk modulesimport { actionExecution } from "@dynatrace-sdk/automation-utils";export default async function ({ execution_id, action_execution_id }) {// get the loop item for the action executionconst actionEx = await actionExecution(action_execution_id);// log the current value of the loop itemconsole.log(actionEx.loopItem)}
Example using the client-automation SDK to access loop item
// import of sdk modulesimport { executionsClient, actionExecutionsClient } from '@dynatrace-sdk/client-automation';export default async function ({ execution_id, action_execution_id }) {// get the loop item for the action executionconst actionEx = await actionExecutionsClient.getActionExecution({ id: action_execution_id });// log the current value of the loop itemconsole.log(actionEx.loopItem)}
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:
- The JavaScript modules need to be valid ECMAScript modules
- They run within the context of the Dynatrace JavaScript runtime and it's respective compatibility.
- Only modules from allowlisted URLs can be loaded. (Settings > Preferences > Limit outbound connections.)
- Imports may not exceed 6MB in size (combined).
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.
-
Add the XMLJSON librabry URL to allowed outbound connections.
- Go to Settings > Preferences > Limit outbound connections.
- Select Add item and add
esm.sh
the allow-list.
-
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 ESMimport xml2js from "https://esm.sh/xml2js@0.6.2";export default async function() {// Dummy XML, can be fetched from your back-endconst 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.