In Power Apps, you can submit multiple records to a data source using different different approaches. It depends on your specific scenario.
We will explore one approach here that involves adding records to a collection using input controls then loop through each record in the collection using the ForAll function, and then inserting the records into the data source using the Patch function.
For this demonstration, we are use the SalesData SharePoint list as data source.
As you can see, there is one record in SharePoint list.
Below is the schema of list.
Lets, take a blank screen and add few input controls Title, Name, Region, Date and Sales and one button control.
After that, give them an appropriate name txt_title, txt_name, txt_region, cal_date, and txt_sales. These control will be used to provide input values to the collection.
Now, go to the OnVisible property of screen and write below formula to create a collection. This formula will initialize the collection with blank values.
[Read about Collection and ClearCollect function]
ClearCollect( col_emp, { Title: "", Name: "", Region: "", SalesDate:Today(), Sales: "" } )
Now, run the app and you can see collection named col_emp is created with blank row except date value as for date field we have give current date.
To see the collection, click on Variables tab, then expand the Collections tab. Click on ellipsis then click on View Table.
Now, add a Data table to see the data stored in collection.
Now, go to the Data Source property of Data table and select collection col_emp.
Now, add the fields that you want to display in data table.
Click on Edit Fields, then click on Add field button. It will displays all the list of available fields. Select the fields and click on Add button.
As you can see, data table display one row. All fields are blank except the date as we initialize the collection with empty values and date field we have provided the current date that’s why it is showing one row.
Now, we will store the data to the collection through input controls. For this, write the below formula to OnSelect property of button.
Collect( col_emp, { Name: txt_name.Text, Title: txt_title.Text, Sales: txt_sales.Text, Region: drp_region.SelectedText.Value, SalesDate: cal_date.SelectedDate } )
Add one record to the collection. As you can see, one row has been added to the collection.
Let’s add few more records in collection.
We will now add these records to the SharePoint list SalesData.
Let’s add one button control and named it Submit.
Now, write the below formula on OnSelect property of Submit button.
ForAll( Filter( col_emp, Name <> "" ), Patch( SalesData, Defaults(SalesData), { Title: Title, EmpName: Name, Sales: Value(Sales), Region: {Value: Region}, Date: SalesDate } ) )
Let’s examine the code shown above, The Default in the Patch function is used to insert a new record to a data source. On top of it, we added the ForAll function, which loops through all of the collection’s rows.
Also, we have filtered out the blank row from collection. Just check that Name field should not be blank.
Let’s click on the submit button to add all the record from the collection to the SalesData SharePoint list.
Now, let’s open the SharePoint list and you can see all the records have been saved in a SharePoint list.
Also Read..
Text function to format date/time or numbers to text string
ShowColumns function in Power Apps
Reset multiple control values and all the controls within a form
Calendar and Clock function in Power Apps