Skip to main content

Notifications

Power Automate - Building Flows
Answered

Alternative to Variables in a ForEach loop

Posted on 15 Nov 2024 22:06:15 by 106
Okay, before you answer with a 'Compose' let me explain the situation. My frustration is high at the end of the week so I hope the community can lend some support over the weekend so I may come back on Monday with some hope. 
 
Problem: 
With Power Automate and 'Variables' they are basically considered 'Global variables' on the flow. Because of this you are required to 'initialize' them outside any Scope or loops. In most cases variables are utilized to hold onto data and reference it inside loops, conditions, or the like. Think of a situation where the variable could be true or false depending on a condition, then later in the flow/loop you need to output the true or false. You can't do this with compose or individual actions because you would need to know which action to reference, the true action or false action. 
My particular issue is that I have a complex flow. It has multiple variables and variable types. I have a string variable with HTML that is appended to, string variables that are updated or changed based on conditions, and null values that get filled in during the process. I also have a foreach loop that processes the data concurrently, because of this the variables are overwritten at different points in the process manipulating the data. 
 
Solution???
I know that 'Compose' is what everyone says to use, but that would only work if literal references were possible. It won't work if i use conditions, it won't really work if i need to append string to them. All of it comes down to compose needs a direct reference and the whole point of using a variable is to avoid that. 
 
I am drawing so many blanks and dead ends. Any help would be greatly appreciated. 
Categories:
  • Verified answer
    AsaurdiffADTill Profile Picture
    AsaurdiffADTill 106 on 27 Nov 2024 at 18:01:44
    Alternative to Variables in a ForEach loop
    I wanted to mention that i went with 'Mark's' option in a way. 
     
    Because I had a LOT of 'append' to's and re-referencing variables I was able to use 'Child' flows in this process. I used the Parent to grab the reference table data/lists and i sent that into a Child Flow using a Base64 encoded string. Then in each child flow i processed the data using variables singularly. Then i pass a json formatted object of key/value pairs with the value, also being a base64 encoded string. Then in the parent i decode the values and apply them to compose actions as the 'variables' and distribute inside the loop. 
     
    Pros: 
    This process does fix my issue and minimizes the runs on the parent flow, So i can send batches to the parent flow. Since this is technically the most complex part of the process it save exceptional amounts of runtime when concurrency is enabled. 
     
    Cons: 
    This will see the child-flows run PER item in a batch PER child-Flow ( I have 5 child-flows running parallel in the parent-flow) BUT considering the process takes less then 5 seconds PER run, I am less worried. NOTE: these child-flows have minimal if no actions that do calls. Its mostly formatting the data from the parent-flow into a formatted HTML string. 
     
    Overall this process still saved exponential amounts of processing time :) Thanks Mark for getting me into the right mind set for this. 
  • Verified answer
    Mark Nanneman Profile Picture
    Mark Nanneman 324 on 15 Nov 2024 at 22:31:35
    Alternative to Variables in a ForEach loop
    I've built complicated flows like this in the past that generated very complex html strings for email reports--html that had links to records, even embedded thumbnail images relevant to each record, special highlighting of values in certain ranges, flag icons, etc., etc., in the report. 

    Once or twice I've had the time to think about a way to refactor them to run faster.  I found that in some cases I could get rid of the for each loop all together.  

    The way I did this was using a "Select" operation, and putting all of my conditions into the expressions in the Select action, then doing a Join on the Select output array. 

    Often I would create string "templates" and save them in compose actions above my select.  I would add my own variables/placeholders to these templates (e.g. "<a href="{link}">Open {recordName}</a>").  Then in my Select expression I would do Replace() functions to find my "placeholders" in my "templates" and replace them with an "item()?['property']" expression.

    Whatever logic you can accomplish in a "Condition" action, you can also accomplish in an expression in power automate.  The downside is it's painful to write it all out in the Power Automate Flow Designer, so I'd just write the code in a Notepad or VS Code even, then copy paste.

    It hurt my brain to plan it out and test it, but when it was finished it ran insanely fast. 

    I don't know if it'll help in your case, but just giving an outside the box idea!

    Power Platform Developer | LinkedIn: Mark Nanneman | Blog: Power Stuff  | YouTube: Mark's Power Stuff  | Buy me a coffee
  • Suggested answer
    tom_riha Profile Picture
    tom_riha 9,930 on 15 Nov 2024 at 22:21:20
    Alternative to Variables in a ForEach loop
    Hello,
     
    I dealt with a similar situation in the past, and the solution was to store objects in the variables where I could always identify later which value from the variable is relevant for the current instance of the loop. I explained the solution in this article.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,137

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,813

Leaderboard