A challenge in Microsoft Flow is to obtain specific data from a collection of objects (Like Dynamics 365 records) and use some data from those records within a single HTTP request. Why not make multiple HTTP requests within the ‘Apply Each’ loop I hear you ask? Well my friend, because there could be 100 records or more, that’s 100 hits to an endpoint which is really unnecessary, time-consuming and prone to error. Best Practice is, if the scenario allows it, to send as few requests as possible.
Microsoft Flow makes it quite easy to be able to iterate over a collection of entities, however, there are certain steps to take to be able to ensure you can obtain data from those items and then add that data to a single payload. This post is a walkthrough on how to achieve this.
Create a Flow which iterates over a collection, similar to what this previous post covered. You should have your starting point and then a condition in where the ‘Collection’ and the ‘Apply to Each’ blocks are.
Create an Array Variable
Insert an Array Variable, by searching for ‘Variable’ within actions. Add this after the trigger in the Flow, as shown in the screenshot below.
Ensure you have added the ‘Apply Each’ block
and select the ‘Value’ from the previous collection element (built in the previous
post). Now add a ‘Compose’ block within
the loop. This composes an arbitrary object which, within the loop, will update the Array variable on each pass of x number of records from your collection. After the Compose action, select the ‘Append to Array Variable’ action. Select the Name from the dropdown, the variable you created, to begin with. Then add the ‘Output’ from the ‘Compose’ block.
Build your HTTP Request
In this next step, this Array variable will be added to the single payload based on the schema you need for the HTTP Request. In the example below, I’m building the object based on the Text Analytics API from the Azure Cognitive Services.
Build the HTTP request as normal, specifying a single pair of curly braces and your collection name within your JSON payload object, together with your dynamic global variable which you defined at the beginning of the Flow. You do not need to add any square brackets in as you would do if you were building this manually. These get added in for you from your Array Variable block, as if you was coding it yourself. (A new array can be created using ‘’).
Below is an example of the output from a Flow run. The HTTP block has been created using a collection of two records and both have been successfully added into the single payload request.
This post has covered how to iterate over a collection element in Microsoft Flow and obtain data from each of those elements to use in another. I hope this has helped. If you have any questions, please leave them in the comments below!