cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MRCL
Advocate V
Advocate V

How to bulk update with ForAll or Patch

I was excited to see bulk update promoted in the latest version. However there is no single example of the syntax needed in the documentation. I tried some obvious alternatives but could not make it work. Please, if you have taken so much effort to include the feature, make a little more effort to show us how it works.

13 REPLIES 13

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)

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?

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.

Hi SCambel.

 

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

Helpful resources

Top Solution Authors
Top Kudoed Authors
Users online (3,442)