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.
Your JavaScript actions can retrieve the result of a previous task and use it for further processing.
// import of sdk modulesimport { execution } from '@dynatrace-sdk/automation-utils';export default async function ({ executionId }) {// load the execution object using the current executionIdvar 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 objectconsole.log('The whole result object: ', myResult);console.log('only one variable: ', myResult.myVariable)}
// import of sdk modulesimport { executionsClient } from '@dynatrace-sdk/client-automation';export default async function ({ executionId }) {// load the execution object using the current executionIdvar config = {executionId, 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)}
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 idsconsole.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.
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.
loopItemValue
export default async function ({ loopItemValue }) {// log the current value of the loop itemconsole.log(loopItemValue)}
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:
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.
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.