1. Tasks
  2. Code

Tasks

Code

Allows the usage of javascript code to return a value

Basic usage

This task allows you to use JavaScript to access data on a stub and return a value which can be used by other tasks and actions.

Here is an example on how this task might look:

loading...

Parameters

language
required
string

The language of the code to be executed is defined in language. Currently the only option is javascript

code_execution
required
object

The code execution is where the custom_data, options and code_block are defined.

Show child attributes
custom_data
optional
object

The custom_data contains values which are accessible by the code block.

code_block
required
string

The code that the task must run.

options
optional
object
Show child attributes
max_execution_time_seconds
optional
number

The maximum time in seconds that the code in the code block will run before being terminated. There is a hard limit of 90 seconds. Setting a value that is more than 90 will still terminate the code after 90 seconds.

Result

loading...

Properties

result

The value that has been returned by the code in the code block.

Global Context

The code task has access to the following in its Global Context:

  • stub
  • stubpost
  • template
  • custom_data

This means it is possible to reference a value stored in the data of the stub by using stub.data.some_variable_name. This applies to everything in the Global Context of the task.

Example using Stubdata:

In the following action we have a key: var_2 with a value of 10 that is stored on the data of the stub that will be multiplied by 10 inside the code block. This value will then be returned in the result of the task after it has run.

In order to do that, we will add a code task and set it up as follows:

editor
        {
  "run_code": {
    "taskapigatewayuuid": "8c2c907b-21ef-4834-825d-8383f0222279",
    "tasktype": "code",
    "params": {
      "language": "javascript",
      "code_execution": {
        "code_block": "function multiply_by_10(){  return stub.data.var_2 * 10 }return multiply_by_10();"
      }
    },
    "name": "run_code",
    "__key": "run_code",
  }
}

      

Explanation:

By referencing stub.data in the code block we are able to access the data that is stored on a stub. This allows the value in the data to be used in the code. In this case we are multiplying the value by 10, the result of which will be returned after the task runs in the result property.

Example using variable substitution:

Inline text substitution works inside the code block works the same as it would anywhere else on Stubber. We can use a value from the action context inside the code block.

In order to do that, we will add a code task and set it up as follows:

editor
        {
  "run_code": {
    "taskapigatewayuuid": "8c2c907b-21ef-4834-825d-8383f0222279",
    "tasktype": "code",
    "params": {
      "language": "javascript",
      "code_execution": {
        "code_block": "function add(x){  return {{stub.data.var_1}} + x; } return add(2);"
      }
    },
    "name": "run_code",
    "__key": "run_code",
  }
}

      

Explanation:

By using inline text substitution we are able to use the value stored in var_1 on the stub. In this case we are adding 2 to the stored value, the result of which will be returned in the result property after the task has run.