cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
euanjfc
Level: Powered On

How to add a new property to an object-type variable in Apply to Each?

Hi

 

Inside an Apply to Each loop's Data-Compose action, if I add an expression using 'addProperty' to add a dynamically created key and value (both created using the loop item data) the object variable remains empty after the loop.

 

An example of the addProperty withing the Data-Compose action might be:

addProperty(variables('obj_var'), item()['key'], item()['value'])

On the loop completing the Data-Compose action Output contains an array of objects each containing a single property, rather than my hoped for single object containing the comma seperated list of key:value property pairs eg.

{ "name":"John", "age":30, "car":null } 

 

Is it possibe to add dynamically created key:values using addProperty to an object variable within a loop?

 

Thanks

6 REPLIES 6
Community Support Team
Community Support Team

Re: How to add a new property to an object-type variable in Apply to Each?

Hi @euanjfc,

 

Could you please share a screenshot of the configuration of your flow?

Could you please share more details about your scenes?

 

I have made a test on my side and I couldn't find a way to add dynamically created key:values using addProperty to an object variable within a loop.

 

Please share more details so we would try to provide a proper workaround for you.

 

Best regards,

Alice

euanjfc
Level: Powered On

Re: How to add a new property to an object-type variable in Apply to Each?

Hi

 

Image of example flow  below. The addProperty expression is -

addProperty(variables('obj_var'),item().item.key,item().item.value)

Example flow

 

and the final Compose output when run shows the empty object variable and the Compose output with an array of objects:

{} [{"key1":"value1"},{"key2":"value2"}]

Thanks

JeffCGrd
Level: Powered On

Re: How to add a new property to an object-type variable in Apply to Each?

The addProperty()  returns a new object.  It doesn't update the variable referenced by the first object.  You have to assign a second object variable the output from addProperty(variable('obj_var'), item().item.key, item().item.value).  Then assign that second variable back to 'obj_var'.  Flow won't let you assign directly to the same var.

Here is a screen shot of my flow.  Configuration is the variable I want to get the new properties. (basically the same as your obj_var)

 

2019-02-16 14_35_26-.png

Highlighted
LorenEdgell_HEI
Level: Powered On

Re: How to add a new property to an object-type variable in Apply to Each?

This is great, but what are the bottom two variable actions? Append to array doesnt work if you have defined an object...

Can you please expand and maybe explain a little more? @JeffCGrd 

TomaszPoszytek
Level: Powered On

Re: How to add a new property to an object-type variable in Apply to Each?

I am doing it the other way round. In a loop of items that I want to append to my object, I'm first using "union" function together with the "addProperty" and then "Set variable" so that ma unioned object is updated:2019-03-01_11h51_53.pngThe "union" operation looks as below:

union(addProperty(variables('var_TempSubscInstanceName_dict'),item()?['Id'],item()?['Name']), variables('var_SubscInstanceName_dict'))

So in the end it is joining together a new object created by "addProperty" and the existing object initiated as variable.

Next I am simply updating (Set variable) my object with the result of the union operations.

 

Regards,

Tomasz

 

JeffCGrd
Level: Powered On

Re: How to add a new property to an object-type variable in Apply to Each?

 

 

Sorry for the late response.  My companies spam filter was catching these notifications.


@LorenEdgell_HEI wrote:

This is great, but what are the bottom two variable actions? Append to array doesnt work if you have defined an object...

Can you please expand and maybe explain a little more? @JeffCGrd 


They are just "Set Variable".  The variables are initialized as Object type.  AddProperty() returns an object and it is assigned to tmpObj.  Then because I want the final values in the configuration variable, I set configuration to the value in tmpObj.

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors (Last 30 Days)
Users online (4,652)