I am attempting to create bulk records with collections and have them display in an editable grid in my PowerApp.
Summary:
To create new bulk records, I create 2 new collections, colBulk and colBulkCopy, to which I patch any specified number of new blank records, varNewRecord, to those collections. colBulk and colBulkCopy are duplicates at this stage.
Bulk Add Button OnSelect:
Set(
varNumber,
varNumber + 1
);
ForAll(
Sequence(Value(NumberOfRecordsToAdd.Text)),
Collect(
colBulk,
Patch(
varNewRecord,
{
ID: varNumber,
'Field 1': Blank(),
'Field 2': Blank()
})));
ForAll(
Sequence(Value(NumberOfRecordsToAdd.Text)),
Collect(
colBulkCopy,
Patch(
varNewRecord,
{
ID: varNumber,
'Field 1': Blank(),
'Field 2': Blank()
})));
I am displaying colBulk in my gallery (colBulk is in the gallery Items property), which displays fields as editable ComboBoxes filled with selections pulled from a SharePoint List. To extract data from the ComboBoxes, I need to use the format ComboBox1.Selected.Result, etc. For OnChange of the ComboBoxes, I select my gallery, Gallery1. When my gallery is selected, it needs to patch all ComboBox selections that were just inputted in the editable grid across ALL the new bulk records that were just added to my collection colBulkCopy. In other words, I need to patch multiple records from one collection (colBulk, which is displayed in my gallery) to another collection (colBulkCopy). I attempted to use a Patch and ForAll formula in my gallery's OnSelect property to accomplish this. I tried a couple different iterations of the Gallery OnSelect.
For all attempts, I am bulk adding 2 new blank records and filling out both records in my editable grid gallery display by making ComboBox selections.
Gallery1 OnSelect Attempt 1:
Patch(colBulkCopy, ForAll(colBulk,
{
ID: ID,
'Field 1': Field1Combo.Selected.Result,
'Field 2': Field2Combo.Selected.Result
}));
Results: 2 new records are created in colBulkCopy with the ID field populated, but Field 1 and Field 2 do not get populated with the inputted ComboBox selections.
Gallery1 OnSelect Attempt 2: Using Gallery1.AllItems instead of colBulk
Patch(colBulkCopy, ForAll(Gallery1.AllItems as new,
{
ID: new.ID,
'Field 1': new.Field1Combo.Selected.Result,
'Field 2': new.Field2Combo.Selected.Result
}));
Results: Same as above - 2 new records are created in colBulkCopy with the ID field populated, but Field 1 and Field 2 do not get populated with the inputted ComboBox selections.
Note: In both the 2 newly added records in both attempts, the ID (created by ID: varNumber in my bulk add button) is the same value for both. It does not generate a unique ID. So both new records in each iteration, for example, could have their ID as 2000. I have attempted to generate a new unique ID by using the following code to generate a temporary table from colBulk and creating a field rowNumber:
With({records: colBulk}, ForAll(Sequence(CountRows(records)),Patch(Last(FirstN(records,Value)),{rowNumber: Value})))
Result: Temp table created of colBulk with rowNumber column populated with a unique ID number for each record in colBulk.
However, I don't know how to incorporate any of that into either of my actual collections.
Also, for both of my Gallery OnSelect attempts, it does not matter if I instead store the value of Field1Combo.Selected.Result in a text label as Value(Field1ComboSelectedResultLabel).Text and use that for the patch to Field 1 rather than using Field1.Combo.Selected.Result. I get the same results.
Originally, I was only adding 1 new blank record and my patch formula worked successfully to patch the inputted ComboBox selections in the new blank record in the editable grid gallery display into colBulk.
Gallery1 OnSelect Single Record Add
Patch(colBulkCopy, LookUp(colBulkCopy, ID=ThisItem.ID),
{
'Field 1': Field1Combo.Selected.Result,
'Field 2': Field2Combo.Selected.Result
});
However, when I attempt to use the above code when adding 2 records, the patch creates 2 new records in colBulk and only writes field values to the first record. It overwrites the first record's values with newly inputted values when the second record is inputted in the gallery, and it always leaves the second record in colBulk blank. I believe this is because "LookUp(colBulkCopy, ID=ThisItem.ID)" specifically points to only one record at a time, so > 1 record bulk patching would not work.
Thanks in advance for the help, it's much appreciated.