cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Coffee654
Frequent Visitor

Make HTTP calls inside loop that filters array?

I'm trying to create some kind of loop/do until that will allow me to filter an array by certain index numbers until all items in the array are processed and I haven't figured out which apply to each/filter/do until/increment variable actions I'm supposed to use. The reason why I have to first filter an array in order to process 100% of the items is that the API only allows a certain amount of items per Batch. In order words, to comply with the API's amount limit, I have to decrease the array's items amounts, make an HTTP call, filter again the Array and make another call until I process 100% of the JSON Array. 

 

Right now, I have the below flow:

Coffee654_0-1649727177718.png

 

The Excel Script action converts a table into an Array. In my example, the array has only 10 collections:

 

 

[
    {
      "Index": "1",
      "Name": "ABC",
      "Date": "2022-01-01",
      "Amount": "100"
    },
    {
      "Index": "2",
      "Name": "DEF",
      "Date": "2022-01-02",
      "Amount": "200"
    },
    {
      "Index": "3",
      "Name": "XYZ",
      "Date": "2022-01-03",
      "Amount": "100"
    },
    {
      "Index": "4",
      "Name": "GHJ",
      "Date": "2022-01-04",
      "Amount": "100"
    },
    {
      "Index": "5",
      "Name": "TRH",
      "Date": "2022-01-05",
      "Amount": "150"
    },
    {
      "Index": "6",
      "Name": "SED",
      "Date": "2022-01-06",
      "Amount": "100"
    },
    {
      "Index": "7",
      "Name": "GFH",
      "Date": "2022-01-07",
      "Amount": "200"
    },
    {
      "Index": "8",
      "Name": "WER",
      "Date": "2022-01-08",
      "Amount": "100"
    },
    {
      "Index": "9",
      "Name": "HYT",
      "Date": "2022-01-09",
      "Amount": "150"
    },
    {
      "Index": "10",
      "Name": "LKJ",
      "Date": "2022-01-10",
      "Amount": "100"
    }
  ]

 

 

 

 

Using this Array as an example, let's assume the API only limits 2 collections per call. In that case, I will need to create some kind of loop that will filter the array by 2 collections, then the HTTP action will extract the values and make the call to a webhook that will trigger the actual API call using the filtered array transferred from the original HTTP action seen in these pics. The loop will repeat this process until the 10 collections are processed. The workflow would look something like this until all 10 collections are processed:

Coffee654_1-1649728000210.png

The Array can have 10,12,15,20, etc. collections. The total amounts are dynamic so the loop will have to be smart enough to adjust the amounts accordingly and process 100% of the collections.

What loop/do until actions do I need to create in order to process 100% of these collections but in installments? (in pairs of 2 in my example). Any help will be greatly appreciated.

 

Thanks!!

 

@loop @http @array @dountil

1 ACCEPTED SOLUTION

Accepted Solutions
DamoBird365
Super User
Super User

Hey @Coffee654 

 

Just seen this challenge 😉

 

You could try the following in an apply to each:

 

DamoBird365_0-1653250905382.png

div(length(outputs('Compose')),outputs('ComposeBatchSize'))

if(equals(mod(length(outputs('Compose')),outputs('ComposeBatchSize')),0),outputs('ComposeBatches'),add(outputs('ComposeBatches'),1))

range(0,outputs('ComposeModNoBatches'))

take(skip(outputs('Compose'),mul(item(),outputs('ComposeBatchSize'))),outputs('ComposeBatchSize'))

 

The initial compose is your Array of 10 objects.  I have a 2nd compose with the required batch size.

 

Using div we can get the no of batches, albeit rounds down, so in the next compose I use mod to see if we have the exact batch size (=0) or if I need to add 1.

 

Then for the apply to each, I create an array from 0 and count by the number of batches, i.e. for 4 batches, 0,1,2,3.

 

Then within the apply to each, for each 0,1,2,3 I multiply by the batch size to get the starting position of the array to skip from i.e. 0*3=0, 1*3=3, 2*3=6 etc and take the batch size of 3 from each skip.

 

Batch 1 is therefore:

DamoBird365_1-1653251172628.png

 

2: 

DamoBird365_2-1653251190362.png

 

etc.

 

Damien

View solution in original post

3 REPLIES 3
DamoBird365
Super User
Super User

Hey @Coffee654 

 

Just seen this challenge 😉

 

You could try the following in an apply to each:

 

DamoBird365_0-1653250905382.png

div(length(outputs('Compose')),outputs('ComposeBatchSize'))

if(equals(mod(length(outputs('Compose')),outputs('ComposeBatchSize')),0),outputs('ComposeBatches'),add(outputs('ComposeBatches'),1))

range(0,outputs('ComposeModNoBatches'))

take(skip(outputs('Compose'),mul(item(),outputs('ComposeBatchSize'))),outputs('ComposeBatchSize'))

 

The initial compose is your Array of 10 objects.  I have a 2nd compose with the required batch size.

 

Using div we can get the no of batches, albeit rounds down, so in the next compose I use mod to see if we have the exact batch size (=0) or if I need to add 1.

 

Then for the apply to each, I create an array from 0 and count by the number of batches, i.e. for 4 batches, 0,1,2,3.

 

Then within the apply to each, for each 0,1,2,3 I multiply by the batch size to get the starting position of the array to skip from i.e. 0*3=0, 1*3=3, 2*3=6 etc and take the batch size of 3 from each skip.

 

Batch 1 is therefore:

DamoBird365_1-1653251172628.png

 

2: 

DamoBird365_2-1653251190362.png

 

etc.

 

Damien

DamoBird365
Super User
Super User

Hi @Coffee654 

 

How did you get on?  I noticed a wee typo, used sum instead of add - updated the sample expressions above.  I'm getting PowerApps and Automate mixed up.

 

Damien

@DamoBird365 as always your solution worked perfectly. Thanks, mate!

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power Automate Designer Feedback_carousel.jpg

Help make Flow Design easier

Are you new to designing flows? What is your biggest struggle with Power Automate Designer? Help us make it more user friendly!

Top Solution Authors
Top Kudoed Authors
Users online (1,908)