Function nodes
Learn about functions in workflows.
A function in Actioner is a building block written in JavaScript. It is a set of statements that performs a task or calculates a value.
Actioner supports using longer snippets of JavaScript via functions. If you're looking to write a lot of JS, you shouldn't inline it between {{ }}
. Functions let you write larger, reusable, blocks of code and enable you to generate some pretty complicated JavaScript snippets.
Functions are setup to extend the capabilities of your workflows. Once created, they are scoped to the workflow. By adding a function, you can manipulate and transform any data generated by the workflow it is added to. If you find yourself using the same or similar blocks of code multiple times throughout your workflow, it might be worth pulling that out into a function.
More than the ability to write longer JavaScript code, functions also enable you to store data in the context
of your workflow and call this data whenever needed in the later nodes of your workflow.
Why use functions?
1. Longer JavaScript snippets
If you're looking to write chunks of JS, creating a function is a good option. You shouldn't inline your code between {{ }}
and keep forcing for one-liner JS codes. Inside a function, you can write larger, reusable blocks of code.
2. Storing and accesing data
Functions enable storing values dynamically and calling them whenever they are needed in your workflow.
If you are looking to build dynamic data as specific to your workflows and re-use that data across other nodes in your workflows, you can add a new function.
With a function, you can write data to the context of the workflow by using .put()
method.
Workflow context
Context is a JSON object that stores the states of data at workflow execution and is referenced with context
.
It is dynamically built as a collection of key-value pairs, where each unique key
is associated with a value
typed string, number, boolean, array or map.
Context is produced when a workflow is executed. When a workflow runs, its context is built incrementally through each step of the workflow and is destroyed when the workflow runs the last node.
Adding data to context
To add data to context, use .put()
method in a function as below:
context.put('key','value')
key
is a string, while value
can be string, number, boolean, array or a JSON object. You can construct the key and its value from the data produced from inputs of manual triggers or the payload of integration or webhook events or the responses to actions that are executed before your function.
If you dynamically refer to a node, the value that is put into the context is the value that node generated while the workflow is running. For example, if you refer to an input in a workflow, the value given to that input is put into the context at workflow execution.
The only way to add data to your workflow's context is through adding a function.
If you add multiple data to your workflow's context with the same key, it takes the value on the last executed function.
Assume that
MyFunction
includescontext.put('myKey','myValue')
.MyNewFunction
includescontext.put('myKey','myNewValue')
.MyNewFunction
is executed afterMyFunction
.
Until MyNewFunction
is executed, context.get('myKey')
returns myValue
. As soon as MyNewFunction
is executed, its previous value is overwritten and context.get('myKey')
starts to return myNewValue
.
Getting data from context
To get data from the context of a workflow, use .get()
method as below:
context.get('key')
Alternative way to access this data is using .
notation after typing context
.
context.key
Actioner also lets you directly access the value in a context by referencing its key. For example, you can access the value of your key by simply typing key
in a function or inside curlies {{key}}
.
To access a key in the context of your workflow, make sure that your function producing that key is successfully executed in an earlier node.