Share and Enjoy !

[Note: this post was originally published in January 2022 and was most recently updated in August 2023. The original title was Handling Dynamic Properties in Power Automate Flow]

 

Have you ever found yourself needing to add dynamic data to an object within a Power Automate Flow? Or need to update a child node inside a JSON object? I certainly have and spent way too much time trying to figure it all out! Here I lay out how to work with dynamic values as well as a little bonus material on how to update child objects (nodes) within a larger parent object.

For a Flow I built recently for a client (link to blog here), I had a need to gather both static and dynamic data to be used in my CSV output. Of course, the static data was simple to add, but figuring out how to add a dynamic property with its associated value was a little harder for me. It’s pretty easy now that I know how though, so thought I’d better document it before I forget all I’ve learned. 😊

Handling Dynamic Properties in a Power Automate Flow

In this fictional example, I’m going to take a list of field names, loop through them and add each to an object variable. Then I’ll also show you how to remove and update properties.

The first thing to do is create a number of variables that we can manipulate. Remember to name them appropriately (and add a note!). Trust me … this will make it SO much easier 6 months from now when you need to revisit the flow and figure out what you did again.

The first variable we need is a string that we set to a comma-delimited list of field names. (In a real situation, these might be held in a configuration list or similar where they could be queried and loaded into this variable.)

Next we need an array variable to hold our field names once we split up the DynamicFields string.  You can also start out with an array of fields if better for your situation.  This variable will be initialized using the following function in order to create the array:

split(variables(‘dynamicFields’), ‘.’)

We also need a variable that will be used to hold our populated data. This should be set to an Object type and initialized to an empty object. It is important to note that an object variable should always be initialized using ‘{}’ and not with a blank value as 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.

After defining our variables, we need to add an ‘Appy to each’ loop with our dynamicFieldsArray variable as the source. Within this loop, we will increment our dummy variable to generate test data, use a ‘Compose’ action to add our property to the object, and finally use a ‘Set variable’ to update the detailsObject with the new value.

Let’s break it down

Our ‘Compose’ action is used to add the property and value to the object and is the key to handling dynamic property values.  This is done using the ‘addProperty’ function found in the Expression tab.

It takes 3 parameters and looks like this:

addProperty(variables(‘detailsObject’), item(), variables(‘dummyFieldValue’))

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

An important step in this whole process is saving the output from the Compose action back to our object variable (detailsObject).  Note that Compose actions only save the computed value to memory so it will be lost if we don’t capture it somewhere.

When the loop is complete, our populated object looks like this:

At this point, we have the data we need and can do any number of things with it.

Other actions you can take on objects

There are other object functions that can be used similarly to the ‘addProperty’ function.

What if our initial dataset has some extraneous data in it that we don’t want and now need to remove? Piece of cake – we can remove a property in much the same way as we add, 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?  For this we use the ‘setProperty’ function in our Compose.  This function works just like the ‘addProperty’ function except that it updates an existing property instead of adding a new one:

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

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:

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

Bonus Material for working with objects

But wait … there’s more.

What if you need to add an object inside this object? This works the same way, except you would pass a child object as the third parameter. To get the below output, your expression could look like this:

addProperty(variables(‘detailsObject’), “currentUser”, { “FirstName”: “Jimmy”, “LastName”: “Buffett })

What if you are trying to manipulate JSON (an object) and you need to change a property inside a child object (node)? For example, you have the above structure in a variable called ‘requestJSON’ and find yourself needing to update the glString property. This is where it can get tricky as the process requires an extra step that isn’t very obvious.

To act on a property in a child object (node), you should append the child node’s name, inside brackets, after the name of your parent object (bolded here):

setProperty(variables(‘requestJSON’)[‘invoice’], ‘glString’, ’55-888-222-55′)

This expression is run in a Compose action, as usual, but note that what is returned from the Compose is only the updated node of the parent object. You would think it would return the entire parent object, but that is not the case. This is the output of the above action:

 

 

 

This means a second Compose is required to merge this output back into the parent. The way to do this is like this:

setProperty(variables(‘requestJSON’), ‘invoice’, createArray(outputs(‘Compose-setGLString’)))

Note how our value for the ‘invoice’ node is wrapped in a ‘createArray’ function. You would think the Compose output would already be an array, but I found that updating without doing this just didn’t work. But by wrapping it in a createArray, I get the following back all neat and clean:

 

 

 

 

 

 

The final step is to set your variable to this output – don’t forget!

Hopefully, this helps you when dealing with objects and dynamic properties in Power Automate. It can be easy at times and sometimes not so easy! 😊

CONTACT THREEWILL TODAY TO LEARN MORE ABOUT THE POWER PLATFORM!

Share and Enjoy !

Related Content: