cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
euanjfc
Frequent Visitor

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
Community Support

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
Frequent Visitor

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

Highlighted
JeffCGrd
Frequent Visitor

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

LorenEdgell_HEI
Regular Visitor

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 

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
Frequent Visitor

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
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Automate Community!

firstImage

Now Live: Power Virtual Agents Community!

We are excited to announce the launch of Power Virtual Agents Community. Check it out now!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

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

sixthImage

Community Summit North America

The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Users online (8,153)