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.
The Run Javascript action does not support expressions in its input to avoid the possibility of code injection.
Task result
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)}