Today I came across a weird feature in Microsoft Flow. I collected a list of files from SharePoint and I was setting a Compose with the title of my files.
Reference Compose inside an apply to each
Table of Contents
In the run history each file was listed and I was quite happy with this. Inside the apply to each step can use this value.

But what if I use this outside the Apply to each after the Apply to each has finished. To my surprise I was getting an array back.

My first thought was: “That is annoying. That can’t be right!”
But you could use this to filter your arrays.

With the condition set to:
startsWith(outputs('Compose_-_File_name'),'t')
This now resulted in an array just giving me the files starting with the letter t.
However I found that the array returned looked like this:
[null,"text","test2",null,"tf78062375",null,null,null,null,null]
Concurrency
It looks like the compose actions inside the apply to each steps are created as an array. Why?
Although by default the Concurrency is switched of on every Apply to each step. It is possible to enable this.
To make sure that things don’t go wrong you could switch this off, however I have found recently that the order of the items is not affected by the Concurrency feature. Performance wise the concurrency feature can really make a difference.

The advanced method
Do you want to take this method further, then read The advanced Pieter’s method in Power Automate
Hi Pieter,
I am facing a weird scenario where I am using the same functionality but outputs(‘compose’) is giving me null. Is there any specific condition that needs to be fulfilled for outputs(‘compose’) to work after setting in Apply to each loop.
Thanks
Compose need to match the name of the compose action.
So most likely
outputs(‘Compose’) instead of outputs(‘compose’)
I checked it but it still not working.
[…] This is a handy trick to join all the changesets together. Instead of initializing a variable, and updating the variable inside the apply to each loop, you can place the compose action just outside the loop to join it together. John Liu mentions this in his video above. He aptly named it the Pieter Method: https://sharepains.com/2019/07/09/compose-actions-inside-apply-to-each-steps-in-microsoft-flow/. […]
Has this changed? When I try this now, and Compose actions inside an Apply to Each are not available at all outside the loop.
Hi Stuart,
Within Dynamics content they are not available but if you type in the expression editor the following expression then it will work:
outputs('name_of_the_action_inside')
An array is an interesting but probably not an unexpected outcome. Look at how the Apply-to-each displays the results for each step. Technically, that is an array. So I am not surprise that after the loop the compose inside the loop became an array.
Hi Pieter,
I have tried to create a compose action, out of “Apply to each” action, but I receive the following error:
Flow save failed with code ‘InvalidTemplate’ and message ‘The template validation failed: ‘The inputs of template action ‘Compose_3’ at line ‘1 and column ‘22174’ cannot reference action ‘Parse_JSON_-_xxxx’. The action ‘Parse_JSON_-_xxxxx’ is nested in a foreach scope of multiple levels. Referencing repetition actions from outside the scope is supported only when there are no multiple levels of nesting.’.’.
Thank you
Claudia
HI Claudia,
This method only works when you don’t have nested Apply to each steps. You can avoid nested apply to each steps by creating child flows.
You can also overcome this crazy limitation by composing the results of each iteration and then inserting that data into a variable while the loop is running. Even this is tricky as it seems traditional variable actions in an apply-to-each loop do not work as expected. Instead, you have to compose the results in each iteration to include all previous iterations. Then set a variable with the cumulative compose output. I am experimenting now to see if the same limitation exists with Do-Until loops but this approach will likely also require the use of variables (which I try to avoid since Microsoft has not made them flow friendly).
Could you please tell me the general principle here? If you use a compose inside a loop, what happens? Does the compose additively join successive values together, creating an array of the items you called compose with? I’m sorry I can’t follow what you’re trying to say.
That is exactly what happens. The objects in the apply to each are merged into an array.
One huge limitation to this is that it ONLY works with a single level of nested loops. It will NOT work if the Compose is nested inside multiple loos, even if you try to access it only one level from the loop it is in. You get an error.
“Flow save failed with code ‘InvalidTemplate’ and message ‘The template validation failed: ‘The inputs of template action ‘Compose_2’ at line ‘1 and column ‘2688’ cannot reference action ‘ComposeTaskStatus’. The action ‘ComposeTaskStatus’ is nested in a foreach scope of multiple levels. Referencing repetition actions from outside the scope is supported only when there are no multiple levels of nesting.’.’.
This is unfortunate, because this is EXACTLY the scenario (where I will have to create, manage, and cleanup a bunch of variables) where I need this capability to make my code cleaner.
This method can almost always work though even when you have nested processes. All you need to do is use Child flows to run the steps inside your apply to each. Then you can use the method described inside the child flow.
Using child flows instead of nested apply to each flows is a good performance improver too. Personally I never nest apply to each steps.