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:
Parameters
required string
language The language of the code to be executed is defined in language.
Currently the only option is javascript
required object
code_execution The code execution is where the custom_data
, options
and code_block
are defined.
Show child attributes
optional object
custom_data The custom_data contains values which are accessible by the code block.
required string
code_block The code that the task must run.
optional object
options Show child attributes
optional number
max_execution_time_seconds 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
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
- actioningContact
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.
Available Modules
The following modules are available for use within the code task:
- crypto
- lodash
- _stubber.utilities
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:
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:
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.
_stubber.utilities
_stubber.utilities
include stubber specific functions. Current available functions are:
- queue_savedata
queue_savedata
queue_savedata
is a _stubber.utilities
function that queues one or more savedata tasks to run after the code task has completed. This allows us to dynamically add savedata actions to run after the code task.
Arguments
The queue_savedata
function requires the following parameters:
required string
fieldname This is the fieldname to which the data will be written to in stub.data
required
savevalue Data value which we want to save
Example code block
This will queue savedatas the value 20
in stub.data.ten_add_ten
as well as 11
in stub.data.one_add_ten
Note that the code task code_block
will still need to return a value in order for the execution to be successful in order to queue the tasks using _stubber.utilities
functions.
The valued that is saved is the value of the variable at the time that queue_savedata
is called, for example:
This will result in the queued savedata tasks to save blue
in stub.data.first_favorite_color
and red
in stub.data.second_favorite_color
queue_merge_object
queue_merge_object
is a _stubber.utilities
function that queues one or more merge_object tasks to run after the code task has completed. This allows us to dynamically queue merge_object tasks to run after the completion of the code task.
Arguments
The queue_merge_object
function requires the following parameters:
required json
merge_data The data structure you wish to store in stub.data.
Example code block
This will merge the following values into stub.data
world
into stub.data.Hello
, Burgers
into stub.data.favorite_food
as well as Stuart
into stub.data.user.username
Note that the code task code_block
will still need to return a value in order for the execution to be successful in order to queue the tasks using _stubber.utilities
functions.