Context - Action's context at action execution
Learn about context and how to use it in your actions.
Context is a JSON object that stores the states of data at action 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 an action is run --either by a workflow or run manually and is destroyed when the run process is completed.
- If it is run by a workflow automatically, context is built incrementally through each step of the workflow and is destroyed when the workflow runs the last step.
- If it is run manually on action bar, or by calling
/actioner
shortcut in Slack or by clicking a button on a Slack notification or on an output; it is destroyed when the action execution finishes.
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. You can construct the key and its value from that action's components.
If you dynamically refer to a component, the value that is put into the context is the value that component takes while the action is running. For example, if you refer to an input, the value given to that input is put into the context at action execution.
Adding a function and adding that function to run steps is the only way to add data to action context. .put()
method is not executed when written in double curlies — {{ }}
, and your action throws an error once it is executed.
If you add multiple data to an action's run context with the same key
, it takes the value on the last executed function.
key
Assume that
- MyFunction includes
context.put('myKey','myValue')
. - And MyNewFunction includes
context.put('myKey','myNewValue')
. - And MyNewFunction is executed after MyFunction.
The value of myKey
becomes myNewValue
at action execution, any previous value is overwritten.
Getting data from context
To get data from run context, use .get()
method as below:
context.get('key')
You can access an action's context through a function or when written in double curlies — {{ }}
.
The data in a context can be accessed
- by that action's requests and functions. The request or the function that accesses an item needs to be executed later than the function that adds that item to context.
- by that action's outputs.
- by the later steps processed in the same workflow. It can be accessed by the conditions and by the parameters (inputs) of the action that is executed in the later step.
You can reference to an action's context, if that action is successfully executed in an earlier step of the same workflow.
For example, if you try to get a value in the first step of a workflow, it would not work since the context has not been created at that point.
Context is created with the successful execution of the first matching action in a workflow. Each executed step can continue to add data to context incrementally. Context is destroyed when the workflow runs the last step.