Functions in Actioner
Learn how to use functions while building functions for your actions.
A function in Actioner is a building block written in JavaScript, containing 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.
When you are designing an action for your custom integration, you can add a new function to extend the capabilities of your action. More than the ability to write longer JavaScript code, functions also enable you to store data in your action's context
and call them whenever they are needed.
Through a function, you can put data to your action's context. Once you put data to the context of your action, that data can be get by the response of your action and by the requests or other functions that will run after your function.
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 action.
If you are looking to build dynamic data and re-use that data across other components of your actions, you can add a new function.
With a function, you can write data to your action's context through put()
method and use get()
method to retrieve data from the context.
Action context
Action context is a JSON object that stores the states of data during action execution and is referenced with context
.
Context is a collection of key-value pairs, where each unique key
is associated with a value
typed string, number, boolean, array or map.
When an action is run, the context is built incrementally through each step and is destroyed when the last step is run and the action produces a response.
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 or a JSON object.
Getting data from context
To get data from the context, use .put()
method in a function 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 an action's 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}}
.
Adding a function and adding that function to run steps is the only way to add data to your action's context.
If you add multiple data to an action's run 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
.
Creating a function
Navigate to your action's Functions tab and click + Add function to create a new function. Give a name to your function.
Adding your function to run steps
After you add a function, do not forget to add it to run steps. A function is executed only if it is added to run steps.
Deleting a function
Click ellipsis … and select Delete to remove a function from your action.