I have a simple setup but just can't get it to work. Understanding the logic behind nested collections is just a little too difficult.
I have simplified my code below:
First I create the general collection with a nested collection under Item2:
ClearCollect(col_test1, {Item1: "Text", Item2: {Child1: "Random"}});
How do add (Collect) a new row to the nested collection under Item2?
I just cannot get it to work.
I get Patch to work, changing "Random" to "Random_new".
Patch(col_test1,LookUp(col_test1,Item1="Text"),{Item2:{Child1:"Random_new"}})
But adding a new row to the nested collection is just impossible for me. Can anyone help me?
Thanks!
Hi @Triplee23 :
Do you want to add records to a subtable in the collection?
My method is to use Split and concat to construct the table to update the data.
The key is to construct a new table as the value to be updated.
Case1:The value of Iterm2 is a single column table
Ceate collection:
ClearCollect(col_test1,{Item1: "Text", Item2: Table({Child1: "Random"})})
Add Record:
Patch(
col_test1,
LookUp(col_test1,Item1="Text"),
{
Item2: RenameColumns(
Split(
Concatenate(
Concat(
LookUp(
col_test1,
Item1 = "Text"
).Item2,
Child1,
"#"
),
"#","Random_new"
),
"#"
),
"Result",
"Child1"
)
}
)
Case2:The value of Iterm2 is a multiple columns table
Ceate collection:
ClearCollect(col_test1, {Item1: "Text", Item2: Table({Child1: "Random"})})
Add Record:
Patch(
col_test1,
LookUp(
col_test1,
Item1 = "Text"
),
{
Item2: ShowColumns(
AddColumns(
Split(
Concatenate(
Concat(
Concatenate(
LookUp(
col_test1,
Item1 = "Text"
).Item2.Child1,
"%%",
LookUp(
col_test1,
Item1 = "Text"
).Item2.Child2,
"%%",
LookUp(
col_test1,
Item1 = "Text"
).Item2.Child3
),
Result,
"#"
),
"#",
"Random_new",
"%%",
"Random_new",
"%%",
"Random_new"
),
"#"
),
"Child1",
First(
Split(
Result,
"%%"
)
).Result,
"Child2",
Last(
FirstN(
Split(
Result,
"%%"
),
2
)
).Result,
"Child3",
Last(
FirstN(
Split(
Result,
"%%"
),
3
)
).Result
),
"Child1",
"Child2",
"Child3"
)
}
)
Best Regards,
Bof
Thanks for the explanation, I got it to work. I understand that the principle here is that I create 2 collections and place one inside the other.
I am planning to use this collection (col_test1) in a nested Gallery, Gallery_Child (Item2: col_test2 collection) inside Gallery_Parent (col_test1 collection). With you solution, I would need as many child collections as records in col_test1.
Keeping your first ClearCollect, is there no way to avoid your second piece of code, meaning adding a new record to Item2 without going through Patch(col_test2)? Can I not add a new record directly referring only to col_test1?
The way you are doing it, will always overwrite the Item2 because Item2 is not a collection or a table. It is record with one column. To add a new child to item 2 you can do the following.
I did this on my end with 2 collections so you can add to them separately.
I added this line on the app OnStart property
ClearCollect(col_test2, {Child1: "Random"}); ClearCollect(col_test1, {Item1: "Text", Item2: col_test2});
And this on a button which adds a new item
Patch(col_test2, Defaults(col_test2), {Child1: "New Random"}); ClearCollect(col_test1, {Item1: "Text", Item2: col_test2});
WarrenBelz
93
Most Valuable Professional
MS.Ragavendar
60
stampcoin
48