Hello Friends,
Welcome back with some new topics. Today, we will discuss about an interview question that is asked in almost all PowerApps interviews.
Ques: How do you insert bulk data in data source using PowerApps?
At first sight, we will say NO WAY. Because Submit Form works on submitting the Form so works for single record. And we use Patch for inserting / updating single record normally.
BUT, do you know that the Patch function is the solution of above question? 🤔
Yes, Patch function inserts data in bulk also.
So, the answer is YES we can do using Patch function.
The complete syntax is-
Patch(datasource,table)
Here datasource is the source where we have to insert the data and table is the collection of records that needs to be inserted as bulk in datasource. Let's try-
- First of all, create a simple list. I had created a basic list with default Title column and added four records.
- Now, Open the PowerApps maker Portal. Create a new PowerApps named "BulkInsertApp". Add-
- Label with text as "Source List"
- Gallery and link with the list (BulkInsertList) (Control Name: galleryBulkInsertList)
- Button with text as "Bulk Insert" (Control Name: btnBulkInsert)
- Label with text as "New Item"
- Input Box with Hint Text as "Enter New Item Here..." and Default Text as blank (Control Name: txtNewItem)
- Button with text as "Add To Local Collection" (Control Name: btnAddToColl)
- The screen will look like above screenshot.
- Now select the button "btnBulkInsert" and choose the OnSelect property. Add below function-
-
Patch( BulkInsertList, collLocalCollectionNewItems ); ClearCollect( collLocalCollectionNewItems, [] );
- Patch will perform the bulk insert activity from collLocalCollectionNewItems to BulkInsertList and then we are using ClearCollect to clear the local collection.
- Now select the button "btnAddToColl" and choose OnSelect property. Add below function-
-
Collect( collLocalCollectionNewItems, {Title: txtNewItem.Text} ); Reset(txtNewItem);
- Here, we are adding the text of text box in local collection "collLocalCollectionNewItems" and then resetting the text box.
- That's all. Execute the PowerApps and add couple of data to local collection.
- I have added "E" and "F" to local collection. Let check the collection-
- Now click on Bulk Insert button.
- Before Click-
- After Click-
- The list status is-
- If we check the local collection now. It should be blank-
- This way, we can insert bulk data in data source using Patch function.
- NOTE:-
- If you insert too much data using this technique then remember the screen remains in block/hang mode till all the data does not get inserted in data source. Therefore, we should avoid large data insertion using this technique.
- Rather we should prepare a separate mechanism where we we insert the bulk data as a single JSON in a temporary list/table.
- There should be a Power Automate linked to that temporary table which will read that input, Parse the JSON and insert the data in data source. This way, your app will not stuck and the purpose will also get achieved.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.