How to Loop Over a Variable In Integration Builder

loop over a variable with Integration Builder
loop over a variable with Integration Builder

The Flow Steps available in Integration Builder Flows are described in the Axway online doc: Flow Step Types. Many of them have been covered in the numerous blog posts and videos on Axway’s developer blog and YouTube channel.

This blog post will describe the Loop Over Variable step. Looping is important for cases where you are processing an array of data (a list) and do operations for each item in the array. For example, get a list of CRM accounts and geocode them, one by one. Looping is often challenging in a graphical editor and the online docs, together with this blog post, will hopefully make it clearer.

Read the online docs to familiarize yourself with the Loop Over Variable step and then let’s get started.

Connector Instance

You will need a connector instance to trigger your flow. If you don’t have one already, then refer to any of the resources described above to create a connector instance that we will use to trigger our flow. Here are some examples:

Create the Flow

We will create a test flow that is intended to illustrate the Loop Over a Variable. Since the Loop Over a Variable step loops over an array, we’ll use the API Request step to retrieve Salesforce accounts based on a trigger from the Salesforce connector. We won’t bother checking the trigger (as we’ve shown in other blog posts) since we are focusing on the looping part and not any specific functionality.

The flow is shown below:

The Flow

You can see that once we get the trigger event, we use the API Request step, getSFDCAccounts to retrieve Salesforce accounts. Refer to the other blog posts references above for the details of this step. It is worth noting that the response we get from this step is already an array and is contained in the response body. We can reference the array using ${steps.getSFDCAccounts.response.body}.

My API Request step is shown below for reference:

API Request Step

The response from this step is as follows:

[
  {
    "LastModifiedDate": "2016-10-28T08:40:21.000+0000",
    "Ownership": "Private",
    "lb_sftest__UpsellOpportunity__c": "Yes",
    "Description": "Genomics company engaged in mapping and sequencing of the human genome and developing gene-based drugs",
    "BillingCity": "Mountain View",
    "lb_sftest__SLA__c": "Bronze",
    "Rating": "Cold",
    "Website": "www.genepoint.com",
    "NumberOfEmployees": 265,
    "Name": "GenePoint",
    "Industry": "Biotechnology",
    "BillingAddress": {
      "city": "Mountain View",
      "street": "345 Shoreline Park\r\nMountain View, CA 94043\r\nUSA",
      "state": "CA"
    },
    "CreatedById": "005i0000001iEVhAAM",
    "OwnerId": "005i0000001iEVhAAM",
    "Phone": "(650) 867-3450",
    "PhotoUrl": "/services/images/photo/001i000000PscewAAB",
    "lb_sftest__SLAExpirationDate__c": "2013-04-28",
    "IsDeleted": false,
    "lb_sftest__NumberofLocations__c": 1,
    "lb_sftest__SLASerialNumber__c": "7324",
    "Sic": "3712",
    "ShippingStreet": "345 Shoreline Park\r\nMountain View, CA 94043\r\nUSA",
    "AccountNumber": "CC978213",
    "SystemModstamp": "2017-11-17T11:30:34.000+0000",
    "Type": "Customer - Channel",
    "BillingStreet": "345 Shoreline Park\r\nMountain View, CA 94043\r\nUSA",
    "lb_sftest__Active__c": "Yes",
    "ShippingAddress": {
      "street": "345 Shoreline Park\r\nMountain View, CA 94043\r\nUSA"
    },
    "CreatedDate": "2013-09-30T21:36:31.000+0000",
    "Id": "001i000000PscewAAB",
    "Fax": "(650) 867-9895",
    "LastModifiedById": "005i0000001iEVhAAM",
    "BillingState": "CA",
    "AnnualRevenue": 30000000,
    "lb_sftest__CustomerPriority__c": "Low"
  },
  {
    "LastModifiedDate": "2013-09-30T21:36:31.000+0000",
    "Ownership": "Public",
    "lb_sftest__UpsellOpportunity__c": "No",
    "lb_sftest__SLA__c": "Platinum",
    "Website": "http://www.uos.com",
    "NumberOfEmployees": 24000,
    "Name": "United Oil & Gas, UK",
    "Industry": "Energy",
    "BillingAddress": {
      "street": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom",
      "state": "UK"
    },
    "CreatedById": "005i0000001iEVhAAM",
    "OwnerId": "005i0000001iEVhAAM",
    "TickerSymbol": "UOS",
    "Phone": "+44 191 4956203",
    "PhotoUrl": "/services/images/photo/001i000000PscexAAB",
    "lb_sftest__SLAExpirationDate__c": "2012-10-26",
    "IsDeleted": false,
    "lb_sftest__NumberofLocations__c": 34,
    "lb_sftest__SLASerialNumber__c": "3479",
    "Sic": "4437",
    "ShippingStreet": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom",
    "AccountNumber": "CD355119-A",
    "SystemModstamp": "2017-09-18T10:30:23.000+0000",
    "Type": "Customer - Direct",
    "BillingStreet": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom",
    "lb_sftest__Active__c": "Yes",
    "ShippingAddress": {
      "street": "Kings Park, 17th Avenue, Team Valley Trading Estate,\nGateshead, Tyne and Wear NE26 3HS\nUnited Kingdom"
    },
    "CreatedDate": "2013-09-30T21:36:31.000+0000",
    "Id": "001i000000PscexAAB",
    "Fax": "+44 191 4956620",
    "LastModifiedById": "005i0000001iEVhAAM",
    "BillingState": "UK",
    "lb_sftest__CustomerPriority__c": "High"
  },
  .
  .
  .
  {
    "LastModifiedDate": "2013-09-30T21:36:31.000+0000",
    "Ownership": "Public",
    "lb_sftest__UpsellOpportunity__c": "Yes",
    "Description": "World's third largest oil and gas company.",
    "BillingCity": "New York",
    "lb_sftest__SLA__c": "Platinum",
    "Rating": "Hot",
    "Website": "http://www.uos.com",
    "LastReferencedDate": "2019-04-25T14:36:38.000+0000",
    "NumberOfEmployees": 145000,
    "Name": "United Oil & Gas Corp.",
    "Industry": "Energy",
    "BillingAddress": {
      "city": "New York",
      "street": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA",
      "state": "NY"
    },
    "CreatedById": "005i0000001iEVhAAM",
    "OwnerId": "005i0000001iEVhAAM",
    "TickerSymbol": "UOS",
    "Phone": "(212) 842-5500",
    "PhotoUrl": "/services/images/photo/001i000000Pscf6AAB",
    "lb_sftest__SLAExpirationDate__c": "2013-04-28",
    "IsDeleted": false,
    "lb_sftest__NumberofLocations__c": 955,
    "lb_sftest__SLASerialNumber__c": "6654",
    "LastViewedDate": "2019-04-25T14:36:38.000+0000",
    "Sic": "4437",
    "ShippingStreet": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA",
    "AccountNumber": "CD355118",
    "SystemModstamp": "2017-04-22T13:30:25.000+0000",
    "Type": "Customer - Direct",
    "BillingStreet": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA",
    "lb_sftest__Active__c": "Yes",
    "ShippingAddress": {
      "street": "1301 Avenue of the Americas \nNew York, NY 10019\nUSA"
    },
    "CreatedDate": "2013-09-30T21:36:31.000+0000",
    "Id": "001i000000Pscf6AAB",
    "Fax": "(212) 842-5501",
    "LastModifiedById": "005i0000001iEVhAAM",
    "BillingState": "NY",
    "AnnualRevenue": 5600000000,
    "lb_sftest__CustomerPriority__c": "High"
  }
]

Then, we enter the loop, called looper. It has two steps inside, consoleLogIndex and consoleLogEntry and then we exit the loop to the step consoleLogExit (and any other steps we would have after that). These are all JS Script steps and are for illustration purposes and don’t really accomplish any useful functions. The step, looper at the bottom of the flow is a reference to the first looper step. Together they signify the start and end of the loop.

Loop Over a Variable

Let’s look at the loop in more detail.

I added a Loop Over a Variable step to the on success output of the getSFDCAccounts step. It is shown below:

Loop Over a Variable Step

There are two parameters that need to be set, Name and List. I named my loop step looper. The list is an array that will be looped over. In my example, I am using ${steps.getSFDCAccounts.response.body} which is the list of accounts from Salesforce retrieved in the prior step. This is already in an array format which is exactly what the Loop Over a Variable step requires.

Note that I could have retrieved the list from a flow variable (that I could have stored in some previous step) using the ${config.variableName} syntax instead.

Now we can add the steps we would like to perform inside of our loop. In my example, as mentioned above, they are consoleLogIndex and consoleLogEntry and they each contain one line each as shown below:

  • console.log(steps.looper.index);
  • console.log(steps.looper.entry);

index is the array index and entry is the array element at that index and is available for all steps inside the loop. This is similar to the Javascript forEach().

To end the loop, we add a step to the onSuccess of the last step in the loop, which is consoleLogEntry in my example, but instead of adding a new step, we select Add From Existing and select our loop step, looper as shown below:

Add Existing Step

consoleLogExit is added to the Loop Over a Variable step onFailure output. This is where the execution will continue when the loop terminates. It contains the following console.log() and again, is for illustrative purposes:

  • console.log(‘Done’);

That’s it! Now we can test our looping flow.

Test The Flow

Cause your flow to trigger and look at the execution of your flow.

In the following screenshot, you can see the successful execution of my flow and the response from getSFDCAccounts. You can see that the response is an array.

Flow Executions

In the following screenshot, you can see the reference to the loop index:

Loop Index

In the following screenshot, you can see the reference to the loop entry, which is the second account in the array:

Loop Entry

In this last screenshot, you can see the loop exits to consoleLogExit:

Loop Exit

Summary

In this blog, we looked at how to use the Loop Over a Variable step in the Integration builder. This will help you create more powerful and flexible flows. Read more, learn how to authenticate Jira Connector with Integration Builder.

Discover my example flow here.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.