Power Platform

Saving datepickers and dropdowns into a collection using the Patch function in PowerApps

In this post I’m going to unravel some of the key points of saving datepicker & dropdown values into a collection, and then displaying them in a gallery.
I have a collection with details of trips, and so I needed a way to add more line items and have them saved in the collection before submitting the form.
Each of my line items has an arrival date and time, as well as a departure date and time.
Note: I do not have a datasource for this, as I will be using the collection to build a JSON string of items. So the collection itself is the destination for all this data.

This is the line item for a trip
The “add new trip” button


I’ve handed the creation of new line items to a simple button.

Here’s the code behind the button


Collect(
tripDetails,
{
lineTripDescription: "Add your trip information here",
lineTripCost: 0,
lineTripDepartureDate:"",
lineTripDepartureHour:"00",
lineTripDepartureMinute:"00",
lineTripArrivalDate:"",
lineTripArrivalHour:"00",
lineTripArrivalMinute:"00"
}
);

As you can see, I use the collect function to create the line items, adding them to my collection called “tripDetails”. You’ll notice that there are strings, and there’s a number for the trip cost. They are just values, remember that.

Now, for the patch function and explanation.
Firstly, I place the patch function on my save button icon. It is attached to the onSelect event/property.

Patch(
tripDetails,
ThisItem,
{
lineTripDescription: tripDescription.Text,
lineTripCost: Value(tripCost.Text),
lineTripDepartureDate: Value(itemDepartureDate.SelectedDate),
lineTripDepartureHour: Value(itemDepartureHour.Selected.Value),
lineTripDepartureMinute: Value(itemDepartureMinute.Selected.Value),
lineTripArrivalDate: Value(itemArrivalDate.SelectedDate),
lineTripArrivalHour: Value(itemArrivalHour.Selected.Value),
lineTripArrivalMinute: Value(itemArrivalMinute.Selected.Value)
}
);

So firstly, you can see that my ‘data source’ is set to the tripDetails collection. The ‘record’ to update is the line item, which uses the ThisItem variable. The important parts to notice are in the values section.
The date and time values need to be entered into the collection as values. i.e. strings. In order to output them as values, I wrapped them in the Value function.
So the datepicker field uses lineitemname.SelectedDate format &
the dropdown menus use lineitemname.Selected.Value format.

That’s not all… I checked my collection and it was indeed saving the values, but I wasn’t getting any output from the gallery items.
My hour and minute dropdowns were showing “00” on every save.
I’d forgotten to set the default values for those fields (or maybe I’d deleted them in my frenzy to fix things)

So select your field, then look on the right hand side at the properties pane and set the Default to the name of the field.

Hope this helps somebody.

Leave a Reply

Your email address will not be published. Required fields are marked *