Using the For each node

You can handle created, retrieved, and updated items in many ways. One way is to use the For each node. This node completes an action for each of the items that were created, retrieved, or updated.

In App Connect, to add a For each node to your flow, click (+) and then select it from the Toolbox tab in the flow editor.

Figure 1. The For each node in the toolbox
The For each node in the toolbox

The following example shows how to use the For each node with a retrieve action. In this example, you can receive a notification for every Jira incident that has a high priority.

Figure 2. Using the For each node and JSONata to process high priority tickets flow tile
Using the For each node and JSONata to process high priority tickets flow tile

Create your flow

Use App Connect to create your flow from a template, or from scratch if you want to explore the steps that are taken to create a flow.

Creating the flow from scratch

You can create a flow from scratch to learn the sequence of steps that are used to create the flow, and to examine the nodes and fields as you go.

Figure 3. Template flow
Template flow
  1. Log in to App Connect.
  2. From the Dashboard, click Create > Event-driven flow.
  3. Enter a name that identifies the purpose of your flow.
  4. Select Toolbox > Scheduler.
  5. Update the following fields:
    • Set Select schedule type to Repeating interval.
    • Set Run every to 1 Minute.
    • Select Also run the flow when it's first switched on.
  6. Click the (+) and then select Jira as your second application.
  7. Select Issues > Retrieve all issues.
  8. If App Connect tisn't connected o your Jira account, click Connect, then follow the instructions to allow App Connect to connect to your Jira account.
  9. Specify the maximum number of issues that you want to retrieve, and what happens if the flow finds more than that number.
    1. To set the limit to the maximum allowed value, click Set the limit to the maximum value Set the limit to the maximum value for Retrieve action.
    2. If the flow finds more issues than the specified maximum, the maximum number of items that are found are processed.
    3. If no items are found, the flow stops with a 404 error code.
  10. To define the logic for each record to process, add the For each node by clicking Add node (+), then Toolbox > For each. For the purposes of this example, you use the fields on the Input tab.
  11. For each incident that has been marked as high priority, you want to receive a notification in the log. You can filter incidents by using a JSONata filter higher order function to process only the incidents that have a high priority. In Select the collection of items to process, enter $filter, then select the $filter() function. This filter returns an array that contains only the values in the following array.
  12. In the array segment, click Insert a mapping The Insert a mapping icon, then expand Jira and Retrieve all issues, and select IssueCollection.
  13. In the function segment, enter function($v, $i, $a).
  14. You now need to use the JSONata string function $contains(str, pattern) to return true if str is matched by pattern. In this example, High needs to match fields.priority.name. Enter the following code {$contains($v.fields.priority.name, "High")}).
    The complete JSONata code is as follows:
    $filter($JiraRetrieveallissues,function($v, $i, $a){$contains($v.fields.priority.name, "High")})
    Figure 4. For each node JSONata code example
    For each node JSONata code example
  15. Update the following fields.
    • Set the Display name to Atlassian JIRA Service Desk Issue (Priority="High" or "Highest").
    • Set Collection processing options to Process all items in the collection sequentially.
    • Set If an error occurs while processing an item to Process all other items and continue the flow.
  16. After the For each node, add a Log node by clicking Add node (+), then Toolbox > Log.
  17. Update the following fields.
    • Set the Log level to Info.
    • Set the Message detail to Atlassian JIRA Issue {{$Foreachitem.id}} {{$Foreachitem.fields.priority.name}}.

      This property logs the Atlassian Jira ID with the priority name in the log.

      Figure 5. Defining the Log node code example
      Defining the Log node code example
      The following example shows the complete flow.
      Figure 6. The complete flow
      Template flow
  18. To test the flow, click Start flow, then click Dashboard to exit the flow.

    Your flow is displayed on the Designer dashboard and is listening for your event, which is the Scheduler to run and retrieve issues.

    Figure 7. The flow tile on the Designer dashboard, which shows that the flow is running and listening for events.
    Example of the flow tile on the dashboard

Test your flow

After a moment, a green tick is shown on the tile to indicate that the flow has run successfully. You can now check the log for entries added for each Jira issue that has a high priority.

  1. Open the Logs view Logs icon.

    You can see the message in the logs as you configured it (Atlassian JIRA Issue [Issue ID] [Priority name]).

    Figure 8. Log results for the flow
    Log results for the flow