Message Image  

Process data from a ‘For each’ node in IBM App Connect

 View Only
Thu July 02, 2020 10:23 AM

IBM App Connect previously introduced the ‘For each’ node to allow you to carry out repeated tasks in both event-driven and API flows. Now you can take that a step further and take the output data from the actions inside your ‘For each’ loops to use in later nodes, or include in an API response.

In the following example, we’re going to create an API flow that allows us to get a collection of payments from a database for each contact we have in Salesforce. In this example the database is an on-premises Db2 system where entries in a database table for payments have a PAYEE value that matches a Contact ID from Salesforce.

After setting up an API flow, we add a node to retrieve up to 100 contacts from Salesforce. We’re also only going to retrieve contacts created after a certain date, whose value is determined by the (ISO 8601 compliant) date value specified in the request message to the API (for example, 2018-02-01 or 2018).

Then we’ll add a ‘For each’ node from the ‘Toolbox’ tab. Set the collection of items to be processed by this ‘For each’ node to be the top item from the retrieve node that we have just created on the ‘Input’ tab; in this case that is Contacts.

In the branch of the ‘For each’ node, we then add another retrieve node which will fetch the payments from the Db2 database, each time restricting its query to those database entries whose PAYEE value matches the ID of the current Salesforce contact. We have access to the properties in a single ‘contact’ object (the ‘input object’) on the ‘For each’ branch to allow us to do this.

If we go back to the ‘For each’ node there is a new ‘Output’ tab. Here we specify the structure of the data as it will appear after the ‘For each’ node. This is done in the same way you have already been able to do in the Set Variable node, or in certain scenarios such as creating Cloudant documents. We add ‘Name’ and ‘Email’ fields of type ‘String’, ‘Payments’ as an ‘Array of numbers’, and ‘TotalPayments’ as a ‘Number’.

After creating the structure we choose ‘Edit mappings’ to select the data we want to appear in our new output collection. Here we use the AMOUNT collection of values from Db2 in the ‘Payments’ field. We also apply the JSONata function $sum() to the AMOUNT value to produce a total value of payments for a single contact which we place in ‘TotalPayments’. Lastly we get the Full name and Email from Salesforce to place in the ‘Name’ and ‘Email’ fields respectively.

On any nodes after the ‘For each’ node in the flow we can access this data in a new collection (array) containing the output from running the ‘For each’ branch for all the Salesforce contacts (input objects to the ‘For each’ node). When mapping a field of another node, the ‘For each’ output is available as an option with any nodes from before the ‘For each’ (such as our Salesforce ‘Retrieve contacts’ node). For example:

We can also use the ‘For each’ output in the API response, allowing consumers of our API to gain the name, email address, collection of payments, and total payments value for any contacts in Salesforce created after a specified date.

If we turn on our API flow we can send a request with, for example, date=2018 and get back a message that looks like

[ { "Name": "Robert Smith", "Email": "rsmith@example.com", "Payments": [107.99, 128.34, 94.49, 235.33], "TotalPayments": 566.15 }, { "Name": "Jane Mandelson", "Email": "jamandy22@example.com", "Payments": [88.20, 167.99, 133.50], "TotalPayments": 389.69 } ]

This is just one of the ways in which you can now take advantage of ‘For each’ output.

2 comments on"Process data from a ‘For each’ node in IBM App Connect"

  1. Matt Bailey May 31, 2018

    A loop counter variable for the For-Each node is not available today for App Connect (Designer), but it is something on our roadmap.

    Reply (Edit)
  2. MATTC May 29, 2018

    Does the App Connect Designer For-Each provide a loop counter variable like the For-Each activity in App Connect Professional Studio?

    Reply (Edit)


#AppConnect
#For-Each-Node