Open Navigation

Data Storage

Store data and create local variables in Tray

Overview

The "Data Storage" connector on Tray allows you to set and get arbitrary data, or perform more complex operations on lists and objects. It works using a key-value store, which means that you can set any type of value you like, using a key to retrieve it. At its core, Data Storage can allow you to work with local variables, easily share data between multiple steps in a custom way, pass data between workflow executions, or even multiple workflows.

Example - Set / Get Operations

In this example we're going to show you a simple use case for the Get and Set operation of the Data Storage connector. In our case we're pulling account details from accounts in Salesforce and adding the Annual Revenue up to send a slack message with the total. In this example we're using a manual trigger but you could set this to a scheduled trigger to receive a daily update.

Here's what your finished workflow will look like

Workflow

Step 1 - Get Account Records

Add the Salesforce connector to your workflow. In the properties panel set the operation to Find Records. Set the Record type to Account and add a field to the fields section. Set the field to Annual Revenue.

Salesforce Properties

For the conditions we want to make sure that we aren't being returned any accounts that have a null value for the Annual Revenue field. This is because we only care about accounts that actually have Annual Revenue.

Salesforce Conditions

Step 2 - Loop Over Records

Next up we'll add in a loop connector to loop over each record and add up the values. Set the list value of the loop connector to the output of the Salesforce connector.

Loop Connector

Inside of our loop connector we'll start with a Data Storage connector. We'll set this to the Get Value operation and set the Key field to total. You can set this field to whatever you'd like but try to name keys in a way that describes what is being stored. We'll set the default value to 0 with the type number. The default value will be returned if no value has been set yet so on the first run it will return 0.

Here's the properties panel for the data storage connector

Get Current

After getting the current total we then need to add in the amount for the record our loop is currently on. Use the math helper to add two values together. Select the Add operation and, using the connector snake(you can learn more about the connector snake here), set the first value to the output of the data storage connector. For the second value we want to use the Account Revenue value of the current record our loop is on. Using the connector snake select the loop connector and select value from the output list. In our example the value is a key/ value pair with the key "Account Revenue" so we will have to add that to the end of our JSON path.

This is the full set up of the math helper connector panel

Math Helper

Now that we have the result of the current total plus the amount for the current record we're on we now want to store this new updated value. Drop another data storage connector after the math helper and set the operation to Set Value. The important part here is to set the key to the same value as you did with the first data storage connector. This will make sure to overwrite the old value with our new updated value.

Here's the property panel

Set New Total

Loop Connectors

Step 3 - Retrieve Final Total

We have now stored our total Account Revenue and can access it with another data storage connector. Drop another data storage connector onto the workflow but this time outside of the loop. We will use the Get Value operation to retrieve the final total. Again set the key to the same key you did with the other connectors so we retrieve the right value.

Get Final Total

Step 4 - Send Slack Message

To send the Slack message with your new total we first want to modify the number to be a currency instead of a plain number. You don't need to take this step but it makes the message a bit more readable. Drop a text helper onto the workflow and set the operation to Format Currency. Type the currency you would like to use in the currency field. We'll set the amount, using the connector snake, to the output of our data storage connector.

Text Helper

Text Helper Output

All we have to do now is add in a Slack connector to handle sending our message. Set the channel to the relevant Slack channel. For the message field we'll inject our total into the message. Using {$} we can access the output of the text helper and embed the total in our message.

Set your up your slack connector like this

Slack

The message in Slack will look like this

Slack Message

Notes on Getting and Setting

Using the Default Value

If the given key is not found then the default value will be returned - which can be set to anything. This is really useful for initialising new lists, strings, or counters that you plan to add things to. For example, now the first Get call for a new key can return an empty list, empty string, or 0, rather than null.

When working with similar data, consider using a list instead of multiple, single items under different keys.

Scope

The namespace and closure convention for Data Storage. You may use a single key once in each scope.

ValueDescription
Current RunAccessible during a single iteration of a workflow
Example: You wish to create a local variable to store a count of items recorded while iterating through a loop.
WorkflowAccessible during all iterations of a single workflow
Example: You wish to record the number of users who have submitted a form using your workflow.
AccountAccessible during all iterations of all workflows associated with a single user account
Example: You wish to assess the volume of API calls that are running through Tray for your organization. You create a single Data Storage element to capture this information for all of your workflows combined

Consider using Current Run unless you specifically need to share data between multiple executions of the workflow or multiple workflows.

Possible Issues

Be aware of possible issues when using Workflow and Account scopes:

  • Data could become inconsistent if multiple executions are concurrently loading, modifying and then saving a value under the same key

Working with lists, aggregating data

While your execution is running, you can use Append to List to store multiple values for later use. You can then get all stored items at the end of your workflow via Get Value.

  1. First, create the list using Set Value or have it created automatically on first append by checking the property 'Create if missing'.
  2. Use Append to List in multiple steps or inside a loop to add more items to the list. You can also append another list.
  3. Once you are finished adding the data, you can use Get Value to load the list.

You can also use Set in List operation to override values under given indices in the list.

Clearing Account level data

Often you will need to clear account level data - particularly when building and testing workflows which might error out and leave values and lists set in a way which means you can't run your workflows again.

It is good practice to finish workflows with storage connectors which reset the data.

You can also consider making a manually triggered workflow specifically to clear account-level data so you can retest your workflows:

manual-clearing-account-data

When doing this be sure to set the data to the correct data type according to how you use it - e.g. an empty array as in the above example, or a number 0, or an empty string, etc.

Important Note on Advanced Operations

Be aware that the Add to Collection and Shift Collection operations should only be used as a last resort, as they have reliability and performance limitations if used in Workflow or Account scope.

They can be used to implement a queue, but if possible, use the AWS SQS connector instead.

Limitations

  1. A hard limit on a value under a single key is 400 KB. Usually the limit will be slightly lower, depending on the size of metadata kept around the value.
  2. Data storage can store objects up to 32 levels of depth. If you need more deeply-nested objects, use a switch in advanced properties in Set Value operation.
Was this article helpful?
Yes
No