cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SCampbel
Level: Powered On

Re: How to bulk update with ForAll or Patch

You can mass batch into a datasource by first collecting the items you want to batch into a temporary collection. Make sure this collection has the same columns as your datasource. Then collect the temporary collection into your datasource. Then clear your temporary collection so that it does not keep getting larger everytime you want to hit the button.

 

Note: Errors will appear if your default values in the datasource do not match the values in the temporary collection. Example of this was the date. The datasource was DateTimeFormat, but when i first collected it was text. Added DateTimeValue in front as per below, and it fixed the error.

 

Example formatting below:

 

Step 1 - collect items in temporary collection:

 

Collect(shoppingcartitems,
ForAll(ShoppingCart.AllItems,
{ItemImage:".\NiskuPowerMinMax_images\"&PartNumber&".png",
PartNumber:PartNumber,
Quantity:QuantityRequired.Text,
IssuedQuantity:"",
Requestor:User().FullName,
DateRequested:DateTimeValue(Text(Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss")),
Project:"",
Module:"",
Picked:"",
PickedDate:""}));

 

Step 2: Collect collection into datasource:

 

Collect(Requests,shoppingcartitems);

 

Step 3: Clear temporary collection:

 

Clear(shoppingcartitems)

Highlighted

Re: How to bulk update with ForAll or Patch

When people ask for better documentation, it's because of useless examples liek this:

Patch(Cars, Table( { Id: 12, Brand: "Audi", Model: "A7" }, { Id: 24, Brand: "Volvo", Model: "S6" } ))

 

Why, in what world ever, would users create a hard coded table with values and pass that into Patch()?

 

How about a useful example, like I have a gallery called MyGallery. It has item collection, MyGallery.AllItems.

User is changing a number of the records in that gallery, and I want user to press a button to update all the records in a single submit.

 

I would expect to be able to do this:

Patch(MyDataSource,MyGallery.AllItems)

and that would update the datasource. But it doesn't, and doesn't give any error messages, so what is the method of updating datasource based on changes to the record collection?

SCampbel
Level: Powered On

Re: How to bulk update with ForAll or Patch

Dave,

 

First off, submit a new question next time on a new thread. But since you asked...

 

MyGallery.AllItems has more items than in my datasource. MyGallery.AllItems contains a "nextarrow" etc. that is incompatible with the column headers in the datasource. When you patch, the columns in the datasource need to match the columns in the item you want to patch. Therefore by collecting those items from MyGallery.AllItems, you are able to use patch as you mentioned above. To do this make a temporary collection:

 

Collect(CallThisCollectionAnythingYouWant, ForAll(MyGallery.AllItems,{Column1Name:PickSomethingInMyGallery.AllItemsToCollectHereForThisColumn,Column2Name:PickSomething,Column3Name:PickSomething....}))

;

Collect(Datasource,CallThisWhatYouCalledItInTheFirstStepAbove)

;

Clear(CallThisWhatYouCalledItInTheFirstStepAbove)

 

Put all the above on one button, and it will do as you require.

If you use a form, just use Submit(Form) and it will put everything into the table (for a new form, continue reading for editform).

 

For editing certain records it is more tricky. You will need to filter your datasource for that record before applying patch. Patch(First(Filter(Datasource,ColumnInDataSource=something,AddMoreFiltersWithCommaAsRequired)),{Column1:ChangetosomethinginMyGallery.AllItems,Column2:ChangetosomethinginMyGallery.AllItems,....})

Patching can edit more than one item in your datasource, but you need to filter for what you want to edit. Don't forget you can LookUp() items if they are not in MyGallery.AllItems (ie. in another datasource).

 

Alternately, you can make a gallery with all the items you want in your datasource, then on the select arrow OnSelect = EditForm(Form). Create a form either on a new page (add navigate to that page after EditForm(Form);Navigate(tothepagewithyourform), or learn how to use UpdateContext() on the on visible section of a form to show it. Fill out the form, and then with the submit button OnSelect = Submit(Form). This will edit items in your datasource, row by row.

Re: How to bulk update with ForAll or Patch

Hi SCambel.

 

Thanks for the very detailed descriptions, that helps a lot.

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (4,729)