Skip to main content

PowerApps updates records while offline? Sweet!

By June 25, 2018June 28th, 2018PowerApps, Pro Tips

Saving data locally to your device requires placing the data you want in a collection. The SaveData() function saves collections and LoadData() reloads them, so we can collect data in a collection and save it to the device while the device is offline.

If you leave the app and come back before connecting to a data source, you can use the LoadData() function to load the collection back to the device from the last time you ran SaveData().

Adding more gummy bear flavors to the same bag

In this example, we’re adding a new gummy bear flavor to a bag of gummy bears. First, we check to see if we’re connected. If we are, we create/update to the data source. If not, we create/update to a collection instead.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”344″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″]

In this screen, we’re patching to the GummyBear SQL database — if we’re not connected, then we collect to the GummyOffline collection.

[divider line_type=”No Line” custom_height=”40″]

EX for Adding New Flavor: If( Connection.Connected, Patch( ‘[dbo].[GummyBear]’, Defaults( ‘[dbo].[GummyBear]’), { GummyBearFlavor : GummyFlavor_txt.Text, Qty : GummyQTY_txt.Text}), Collect( GummyOffline, { Flavor : GummyFlavor_txt.Text, Quantity : GummyQTY_txt.Text,Type : “New” }); SaveData( GummyOffline, “GummyOffline”))

[divider line_type=”No Line” custom_height=”40″]

We’re adding a field to the GummyOffline collection called Type. This flags the entry as a new flavor entry for the bag or updates the quantity of a flavor already in the bag. This will be important later, when we upload the offline data manipulation to the database.

We want to add a Passion Fruit flavor to the bag. Since we’re offline, we collect it in the GummyOffline collection, then we save the collection to the device with the file name of “GummyOffline”.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”348″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″]

Since we added 15 gummy bears in a new flavor, we have to decrease the quantity of the other gummy bears in the bag to make room. So, we update the apple, cherry, and raspberry flavors with quantities decreasing the previous amount by 5.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”353″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″]

Our QTYs should look like this:

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”354″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″]

Apple : 15

Cherry :  20

Raspberry : 15

[divider line_type=”No Line” custom_height=”40″]

EX for Updating QTYs: If( Connection.Connected, Patch( ‘[dbo].[GummyBear]’, First( Filter( ‘[dbo].[GummyBear]’,  GummyBearFlavor = GummyFlavor_txt_1.Text)), { Qty : GummyQTY_txt.Text}), UpdateIf( GummyOffline, Flavor = GummyFlavor_txt_1.Text, { Quantity : GummyQTY_txt_1.Text, Type : “Update”}); SaveData(GummyOffline,”GummyOffline”))

[divider line_type=”No Line” custom_height=”40″]

Updating the database from the local device

We have the desired data with for the new Passion Fruit flavor, but only on our local device. How do we update the database with this information?

Once you’re connected, do a batch Patch to sync the data in the collection to the database, using the ForAll() function. Having icons with different colors is an easy way to make sure you’re. Like so:

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”355″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”40″]

EX for Connection notification:

If( Connection.Connected, RGBA(54, 176, 75, 1) ,RGBA(255, 0, 0, 1))

[divider line_type=”No Line” custom_height=”40″]

The ForAll() in our gummy bear example looks like this:

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”356″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”][divider line_type=”No Line” custom_height=”20″]

This where we need the Type field we added earlier. If this were an update to an already existing record, then Type would flag the change as an “Update” (Type flags new records as “New”).  Once we run this ForAll() function, our updated database will hold the desired data input on a device while it’s offline.

[divider line_type=”No Line” custom_height=”40″]

EX for using the Type field: ForAll( GummyOffline, If( Type = “Update”, Patch( ‘[dbo].[GummyBear]’, First( Filter( ‘[dbo].[GummyBear]’, Flavor = GummyBearFlavor)), { Qty : Quantity}), If( Type = “New”, Patch( ‘[dbo].[GummyBear]’, Defaults( ‘[dbo].[GummyBear]’), { GummyBearFlavor : Flavor, Qty : Quantity}))))

[divider line_type=”No Line” custom_height=”40″]

As you can see below, the updated data on the new flavor and resulting quantities are in the bag.

[divider line_type=”No Line” custom_height=”20″][image_with_animation image_url=”357″ alignment=”center” animation=”Fade In” border_radius=”none” box_shadow=”none” max_width=”100%”]