Share and Enjoy !

Introduction

Have you ever found yourself needing to add dynamic data to an object within a Power Automate Flow? For a Flow I built recently for a client (link to blog here), I had a need to gather both static and dynamic data for my output. Static data was simple to add, but dynamic was a little harder at first. Now that I know how to though, I find it’s a fairly simple thing to do. Hopefully, this blog will make it clear and simple for you as well.

Handling Dynamic Properties in a Power Automate Flow

In this fictional example, I’m going to take a list of fields, loop through them, and add each to an object variable. I’m also going to show you how to both remove and update a property as well.

We first need to create a number of variables. The first is a string that we set it to a comma-delimited list of fields that need to be added to the object. In a real situation, these might be held in a configuration list or another place where they could be queried and loaded into the variable. I’m keeping it simple here for the sake of this demonstration

We need a couple of other variables for this example. The next one is going to be an array variable that we can loop through, which we initialize to the split values of our DynamicFields string. And yes, I could have started out with an array and skipped using a string variable completely. But I chose to start with this so I could demonstrate a more realistic scenario, as we would normally be pulling the list of fields from a config list or similar location and using that output when building our array.

For example, split(outputs(‘Get_DynamicFields_from_ConfigList’)?[‘body/value’][0][‘Value’], ‘,’) will take the output from a Get items action named ‘Get DynamicFields from ConfigList’ that returns a single item and then load that into the array.

We also need a variable that will be used to hold our data. This should be set as an Object type and initialized to an empty object. It is important to note that an object variable should always be initialized to an empty object using ‘{}’ and not a blank value like we do with most of our variables, as an object cannot be acted upon if null.

I also added an Integer type variable – dummyFieldValue (not shown) – that I’m using for generating field values.

Now we need to add an ‘Apply to each’ loop, with our dynamicFieldsArray variable as the source. Within this loop, we will increment our dummy variable for data, use a ‘Compose’ action to add our property, and finally use a ‘Set variable’ to set detailsObject to the new value.

Let’s break it down.

Our ‘Compose’ action is used to add the property to the object. We build this out using the Expression tab and the addProperty function. This function takes 3 parameters and looks like this:
addProperty(variables(‘detailsObject’), item(), variables(‘dummyFieldValue’))

  • variables(‘detailsObject’) = the object to which we are adding the property
  • item() = the name of the property. ‘item()’ references the current iteration of our ‘Apply to each’ loop, which in this case is our field name
  • variables(‘dummyFieldValue’) – The value associated with the property, in this case, our dummy variable, but usually something pulled from SharePoint or another data source

To retain the updated data, we now need to save it back to our object variable using a ‘Set variable’ call. Here you see that we are setting the value to the output from the Compose action. This will ensure the update sticks, as the output from a Compose action is saved to some hidden spot within the Flow and not back into the variable (pretty technical, huh?!).

We continue to loop through the dynamic fields repeating the steps until our object is fully composed. The output when we are done looks like this:

At this point, we can save the object to an array, back to some other field or output, or any number of things. Pretty slick!

But what if our initial dataset has some extraneous data in it that we accidentally added and now need to remove. Piece of cake – we can remove a property in much the same way as we added the property by using a Compose action but this time with the ‘removeProperty’ function. This function only requires two parameters and looks like this:
removeProperty(variables(‘detailsObject’),’EndDate’)

  • variables(‘detailsObject’) = the object from which we are removing the property
  • ‘EndDate’ = the name of the property we are removing

What if we also need to update a property to a new value? This is also fairly simple. We need to use the Compose action again but with the ‘setProperty’ function. This function works just like the ‘addProperty’ function except it updates a value instead of adding: setProperty(variables(‘detailsObject’),’DynamicField1′,’new value here’)

  • variables(‘detailsObject’) = the object to which we are adding the property
  •  ‘DynamicField1’ = the name of the property to update
  •  ‘new value here’ – The value to update the property with, a simple string in this example

And remember, after each of these Compose statements, we need to use a ‘Set variable’ to save the output from the Compose back to our detailsObject variable to retain the change. After running these two commands, the output now looks like this:

Conclusion

Hopefully, this helps you when dealing with dynamic properties in Power Automate. It turns out to be fairly simple once you dig in. It’s all that other logic needed around these steps that complicates everything! 😊

For reference, here is a quick view of the flow showing all the steps mentioned above.

CONTACT THREEWILL TODAY TO LEARN MORE ABOUT THE POWER PLATFORM!

Share and Enjoy !