See also: Loop Collection [Connector Reference]
Many workflows on the Tray platform involve looping through a list of items, and performing a logical operation based on each item. For example, you may want to:
- Get a list of contacts recently created in your CRM
- Notify a Slack channel for every user that matches a certain criteria
- Loop through each row of a Google Sheet, checking for new rows
The workflow builder allows you to handle lists of just a few items, or even to iterate over every single user in an account.
Loop Using a JSON Path
Let's start with a basic example. We want to get a list of 5 contacts from a CRM (we'll use ProsperWorks in this example), and then send a Slack message to a channel for each one of them.
Step 1. Create a new workflow, with a Manual Trigger.
- Add the ProsperWorks connector to the workflow.
- Select the Search People operation.
- Ensure Page Number is set to 1 (A single page of results will be returned).
- Set the Page Size to 5 (this specifies the number of results that will come back on Page 1).
- Add a Title to the ProsperWorks connector by double-clicking on the title. When adding a large number of connectors to a single flow, this will ensure the logic in your flow can be read from a high-level.
Step 2. Add a Loop Collection connector to the workflow. Loop through the list of people that is returned by ProsperWorks.
- Drag the Loop Collection connector to the + below ProsperWorks.
- In the Loop Collection Properties panel, drag a Snake from the List input to the ProsperWorks connector.
- Select Results from the Dynamic Output Schema that is returned.
For more information on why we selected Results, and what would happen if we selected one of the other elements from the Dynamic Output Schema, see: Working with Data Structures.
Step 3. Send a Slack message to a specified channel for each person that is returned from ProsperWorks.
- Drag a Slack connector into the first item for loop-1 (you'll see this label in grey under our loop 'For each person').
- Ensure you have authenticated Slack in your account. Then, select the appropriate Channel from the drop-down menu.
- Navigate to Message in the Slack Properties panel. Drag a Snake from Message to loop-1.
- Select Name from the Dynamic Output Schema.
Q: Why did we pass in the Message from loop-1, and not the ProsperWorks connector?
A: In a previous step, we linked the Loop Collection connector (loop-1) to a list (Results) returned by the ProsperWorks connector. Now, the loop signifies each successive item in the ProsperWorks list Results. Therefore, we can access the data from a single item in Results by dragging a Snake directly to loop-1.
The Loop Collection connector (loop-1) will execute for each item in the list it is passed. Any operations within the loop will execute for each item in the list that is being passed into loop-1.
Step 4. Click "Run Workflow Now". Open up your Slack to see the users come in!
Loop Using a Custom Array
Pass in a custom list of data, and perform a custom operation with each item using a loop. In the example below, we create Asana tasks for each stage in a sales process.
- Add a Loop Collection connector to your workflow.
- Ensure the Operation selected is Loop Array.
- In the type selector dropdown, select Array.
- Select Add Item, and add some items of your choice. For this demo, we added <Discovery Call>, <Product Demo>, <Close Deal>.
- In your loop, add and Asana connector (remember to label your connectors to make your workflow easier to read should you decide to edit it later).
- In the Asana connector, select the operation Create Task.
- Pass in the task name by dragging a Snake to loop-1.
- Select a Workspace to create your tasks in, as this is a required field (denoted by the red asterisk).
REQUEST FREE TRIAL
Share A Little Information To Request Free Trial