DEV Community

Cover image for Power Automate - Dynamic Variables
david wyatt
david wyatt Subscriber

Posted on

Power Automate - Dynamic Variables

Let's start with what a dynamic variable is, then why it's so cool.

A dynamic variable is when we are able to select the variable based on another variable, i.e. dynamically construct the parts of an expression.

In Power Automate if we want to choose between 2 values, we can use either the if() expression, or Conditions/Switches. But this has the drawback of either binary choices, or over complexity (just imagine a Switch with a 100 options).

But what you can do is select a variable/action property dynamically, let me give an example.

Lets say if a variable = Integer then use SharePoint Field_Integer, if it is String then get SharePoint Field_String.



if(equals(variable('var'),'Integer'),
  outputs('Get_items')?['body/value']?['Field_Integer']
,
  outputs('Get_items')?['body/value']?['Field_String']
)


Enter fullscreen mode Exit fullscreen mode

Yet with dynamic variables we could do this



outputs('Get_items')?['body/value']?[concat('Field_',variable('var'))]


Enter fullscreen mode Exit fullscreen mode

So any property can be set from a variable.

outputs('Get_items')?['body/value']?[YOUR VARIABLE]


The main time I use this is when I update a html template file with lots of placeholders. Before I would do the below



replace(
    replace(
        replace(
            replace(
                replace(
                    replace(
                        replace(
                            replace(
                                replace(
                                    replace(
                                        replace(
                                            replace(
                                                replace(
                                                    body('Get_file_content_template'),
                                                    '{name}',
                                                    outputs('Get_item')?['body/Title']
                                                ),
                                                '{sdd}',
                                                base64ToString(base64(body('HTTP')))
                                            ),
                                            '{type}',
                                            outputs('Get_item')?['body/App_x002f_Automation/Value']
                                        ),
                                        '{business}',
                                        outputs('Get_item')?['body/BusinessTeam']
                                    ),
                                    '{geo}',
                                    outputs('Get_item')?['body/Location/Value']
                                ),
                                '{solution}',
                                outputs('Get_item')?['body/ShortDescriptionoffunctionality']
                            ),
                            '{storage}',
                            body('Create_HTML_table_data')
                        ),
                        '{trigger}',
                        if(equals(outputs('Get_item')?['body/Trigger/Value'],null),'',outputs('Get_item')?['body/Trigger/Value'])
                    ),
                    '{connections}',
                    body('Create_HTML_table_con')
                ),
                '{privileges}',
                body('Create_HTML_table_privil')
            ),
            '{output}',
            body('Create_HTML_table_out')
        ),
        '{data}',
        outputs('Get_item')?['body/ShortDescriptionofDataHandled']
    ),
    '{owner}',
    outputs('Get_item')?['body/Author/Email']
)


Enter fullscreen mode Exit fullscreen mode

To update the below file:

html template

Which although efficient, is a nightmare to read let alone maintain/update.

But with dynamic variables I can create a reference table, loop over it and use it to update each placeholder.

sharepoint list

The placeholder is the text in the template, the variable is the property I want to replace it with.

I store the template document in a variable, then loop over each item in the list replacing the placeholders. As you can't self reference a variable I pass it to a compose first and then set the variable to the compose.

flow loop



replace(outputs('Reset_variable'),
    items('Apply_to_each')?['Title'],
    outputs('Get_item')?['body']?[items('Apply_to_each')?['Variable']]
)


Enter fullscreen mode Exit fullscreen mode

Now this works ok if every property is from same action (e.g Get_item), but what happens if they are from multiple. Well in that case we need to create a consolidation object, and then pass that.

consolidation object

We then reference that output from the object (in this case a compose):



replace(outputs('Reset_variable'),
    items('Apply_to_each')?['Title'],
    outputs('Object')?[items('Apply_to_each')?['Variable']]
)


Enter fullscreen mode Exit fullscreen mode

I must admit this is a niche feature (I am a big advocate of code instead of actions, so my above example goes against my own principles). But there will be certain scenarios where having this in your back pocket will rescue you (e.g. a dynamic list for SharePoint http action). And others where the extra flexibility is gold (imagine having multiple different templates continually updated, we can filter the placeholder list and return different placeholders for each).

And yes, my idea of cool maybe a little 'unique' 😎

Top comments (1)

Collapse
 
sdelisle profile image
Simon de Lisle • Edited

So I use the Replace technique you showed above to generate HTML emails so this interests me A LOT. I too have upto 30 Replace pairs in a single function in my flow and I use an Excel spreadsheet with VBA to create it - and yes, it is a nightmare to edit! So yes, this interests me!

However I do not quite understand the very last part where you show the Object inputs image and the Replace image. Pardon my inexperience here but I don't understand how you loop through each Placeholder/ReplaceWith pair in the Object. Sorry if I am missing something obvious.

BTW now going through the rest of your posts!