Power FX: Dynamically access an object property with a variable

I’m kind of new with Power Apps, and it’s been very difficult to find how to access an object property using a variable.

In a nutshell, here are to access it:

With(
  { tempData: ParseJSON(JSON(ThisItem, JSONFormat.IgnoreBinaryData & JSONFormat.IgnoreUnsupportedTypes)) },
  Concat(
    ForAll(
      ColumnArray As ColumnName,
      ColumnName.Value & "=>" & Column( tempData, ColumnName.Value ) & "<br>"
    ),
    Value
  )
)

I’m transforming my record ThisItem to an UntypedObject to make it work with Column(), using JSON and by removing the problematic properties (thanks to JSONFormat.IgnoreBinaryData & JSONFormat.IgnoreUnsupportedTypes).

Then I iterate my array (a.k.a ColumnArray) that contains the property I need, and I use Column( tempData, ColumnName.Value ) to extract the column from the record!

My use case: I defined a collection with a list of columns, then I have a Gallery in which I have an HTMLText component that must display the columns from ThisItem based on the collection I defined before.

So, in App screen, I defined my collection like that:

ClearCollect(ColumnArray,["ID", "Title", "Description", "Target_x0020_Start_x0020_Date", "AssignedTo"]);
ClearCollect(UserColumns, ["AssignedTo"]);

Then in my Gallery, I have an HTMLText object with the below content:

"<div style='display:grid;grid-template-columns: 100px 50px 1fr 1fr 100px 150px;gap: 0px;height:65px;max-height:65px;overflo:hidden'><div style='"&cssTdStyle&";display:flex;justify-content:center;'><button type='button'>Edit</button></div>" &
With(
  { tempData: ParseJSON(JSON(ThisItem, JSONFormat.IgnoreBinaryData & JSONFormat.IgnoreUnsupportedTypes)) },
  Concat(
    ForAll(
      ColumnArray As ColumnName,
      "<div style='"&cssTdStyle&"'>" &
  If(
    ColumnName.Value in UserColumns,
    Column( tempData, ColumnName.Value ).DisplayName,
    Column( tempData, ColumnName.Value )
  )
  & "</div>"
    ),
    Value
  )
)
& "</div>"

This way I can make my display dynamic, based on a list of columns (that can be populated using a Power Automate Flow). I’ll have to identify which columns are User type, or similar ones, in order to access to the next property (like .DisplayName).

Leave a Reply

Your email address will not be published. Required fields are marked *

*