cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
n8greene
Advocate II
Advocate II

Create multiple duplicate records

Hi-

What is the best way to create x number of duplcate records?

 

Background: I am building an inventory tracking application. There is a record created for each item so each item can be tracked individualy. If we buy 100 widgets, I need a process that will create 100 records each containing the same data. To do so, I will have a field where someone enters the number of widgets purchased and a button to add them to the sytsem. What code should be in that button?

1 ACCEPTED SOLUTION

Accepted Solutions
v-xida-msft
Community Support
Community Support

Hi @n8greene,

 

Could you please share a bit more about your scenario?

Do you want to create multiple duplicate records at the same time within PowerApps?

 

If you want to create multiple duplicate records at the same time within PowerApps, I think the ForAll function and Patch function could achieve your needs.1.JPG

 

Set the OnVisible property of the first screen of my app to following formula:

ClearCollect(NumberCollection,["1","2","3","4","5","6","7","8","9","10"])

On your side, the count rows of the NumberCollection must be more than the number of widgets purchased that you typed. You should type the following formula:

ClearCollect(NumberCollection,["1","2","3","4","5","6","7","8","9","10",...,"98","99","100",...,"200","201","202",...])

 

Set the OnSelect property of the "Create" button to following formula:

ForAll(
FirstN(NumberCollection,Value(TextInput1.Text)),
Patch(
'20180907_case1',
Defaults('20180907_case1'),
{
Title:"PowerApps Test",
Part_x0020_Name:"Part1",
Price:30
}
)
)

Note: The '20180907_case1' represents the SP list data source within my app.

 

On your side, you should type the following formula:

ForAll(
FirstN(NumberCollection,Value(TextInput1.Text)),
Patch(
'YourDataSource',
Defaults('YourDataSource'),
{
Title:"xxx",
Column1:"xxx",
Column2:"xxx",
...
}
)
)

 

The GIF image as below:Test1.gif

 

 

 

More details about the ForAll function and Patch function in PowerApps, please check the following article:
ForAll function, Patch function

 

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
Drrickryp
Super User
Super User

Hi @n8greene,

 

Inventory control is a common business scenario and you likely don't have to re-invent the wheel.  There are examples of inventory control apps in both MS Access and Powerapps.  In Powerapps there is a template called Asset Checkout that likely can be modified to fit your needs. If you allow it to create the app and then choose Make my own app at the top of the screen, it will create the data sources that you can modify to meet your needs.  In the examples that I have seen, there is a list of items  and a quantity is assigned to each item rather than creating multiple duplicates of the same item. An inventory control number can be assigned to it once it is sold. https://www.c-sharpcorner.com/article/build-asset-checkout-app-using-microsoft-powerapps/ has  step by step instructions on how to create the app. Veronica Ward has a blog post that you could review on how to create an inventory app as well. https://powerapps.microsoft.com/en-us/blog/creating-a-swag-inventory-app/.  I hope this could help you get up and running fast.

I have a true need to have a one-item one-record relationship, mostly based on my need to show counts of product stored in multiple locations within multiple cities (many to many displayed in galleries using GroupBy). This is all working very well but I have yet to come up with a very elegant way to create multiple records all at once, so this question is still open if anyone has any suggestions. Thanks

v-xida-msft
Community Support
Community Support

Hi @n8greene,

 

Could you please share a bit more about your scenario?

Do you want to create multiple duplicate records at the same time within PowerApps?

 

If you want to create multiple duplicate records at the same time within PowerApps, I think the ForAll function and Patch function could achieve your needs.1.JPG

 

Set the OnVisible property of the first screen of my app to following formula:

ClearCollect(NumberCollection,["1","2","3","4","5","6","7","8","9","10"])

On your side, the count rows of the NumberCollection must be more than the number of widgets purchased that you typed. You should type the following formula:

ClearCollect(NumberCollection,["1","2","3","4","5","6","7","8","9","10",...,"98","99","100",...,"200","201","202",...])

 

Set the OnSelect property of the "Create" button to following formula:

ForAll(
FirstN(NumberCollection,Value(TextInput1.Text)),
Patch(
'20180907_case1',
Defaults('20180907_case1'),
{
Title:"PowerApps Test",
Part_x0020_Name:"Part1",
Price:30
}
)
)

Note: The '20180907_case1' represents the SP list data source within my app.

 

On your side, you should type the following formula:

ForAll(
FirstN(NumberCollection,Value(TextInput1.Text)),
Patch(
'YourDataSource',
Defaults('YourDataSource'),
{
Title:"xxx",
Column1:"xxx",
Column2:"xxx",
...
}
)
)

 

The GIF image as below:Test1.gif

 

 

 

More details about the ForAll function and Patch function in PowerApps, please check the following article:
ForAll function, Patch function

 

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hi Kris-

 

Thank you. The best solution I'd come up with was similar, but your approach is cleaner, and I will use it. My biggest concern was that I was missing some simple function that I'd overlooked, but it turns out this does require a bit of a work-around, as you described.

 

Here was my similar working solution (prior to seeing your post). I had actually set up a SharePoint list (called InventoryCountTable) with 150 dummy records that I was using to create the collection I could use with ForAll. Your idea of building that collection right in PowerApps, though, is better. This is the code from my Add Items button:

 

 

code.png

Kris (and anyone else interested in this topic)-

 

If anyone is running into SLOWNESS with this approach, here's a way to make it go faster:

 

Instead of writing each new record one-at-a-time directly to the datasource in the ForAll loop, write to a tempoarary collection. 

 

Then use Patch to insert the entire collection into the data source (see the last few lines of my code below). 


This worked MUCH faster in my application, perhaps because it does not kick off a bunch of formulas each time a new record is added.

 

UPDATE:  In my code below I used "Defaults(InventoryItemRecord)" in the collect command, but I later found it worked better with that removed. (It was causing duplicate empty Records). 

 

Here is the code (in two screenshots, bottom and top)

CodeTop.PNGCode2.PNG

@n8greene  great solution! it works like charm.

however, i have one more level of complexity. I have a parent Child relationship and the requirement is to duplicate both parent and child records associated.

 

using your method i was able to duplicate parent item in Sharepoint list. 

would it be possible to know how to clone Child records for each duplicate Parent record?

thanks.

Sofie_D
Responsive Resident
Responsive Resident

 


@jshah wrote:

@n8greene  great solution! it works like charm.

however, i have one more level of complexity. I have a parent Child relationship and the requirement is to duplicate both parent and child records associated.

 

using your method i was able to duplicate parent item in Sharepoint list. 

would it be possible to know how to clone Child records for each duplicate Parent record?

thanks.


@jshah I'm also trying to duplicate an item with multiple child items. Duplicating the parent works, but can't get it to work for the child items. Have you gotten this to work?

I've tried the following:
ClearCollect(colTempRoutingDupl,Filter(ROUTINGS, EindProduct = combobox1.Selected.EindProduct));UpdateIf(colTempRoutingDupl, 1=1, {EindProduct: txtInput1.Text});
ForAll(colTempRoutingDupl, Patch(ROUTINGS, Defaults(ROUTINGS), LookUp(colTempRoutingDupl, ID = colTempRoutingDupl[@ID])))

In which ROUTINGS are child items with 'EINDRODUCT' linking to the parent. I get an error on the Patch function that says '{Attachments}: field required'  but none of those items have any attachments

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (1,192)