So you developed an app that handles offline and online using LoadData and SaveData, and then you come across this weird behaviour where screens are cleared out. This is exactly what happened to me with an inherited app.
The solution architecture
Table of Contents
I inherited an app that has one table in dataverse and multiple screens update data in this table, while the user may go offline and online.
If this doesn’t sound like a nightmare …
On each of the screens, I had code that looked like this.

So the If function will first check if we are online or not. If we are online then we can update the database. Otherwise we will just update a variable, using the Patch function.
Without really thinking about the consequences of this code, it all looks ok. But there is a major bug there.
Imagine if a user goes offline on screen 1, then the go online on screen 2 and hit that save button. My gblActiveForm variable will get the data from the database, which doesn’t have the latest version of my updates made on screen 1.
For simplicity sake, I kept the SaveData code out of the above example.
Fixing the cleared out data issue
The code for when we are offline will remain the same. We just need to make sure that our variable remains up to date when we update the database.
The following code fixes the issue.
If(
Connection.Connected,
Set(gblActiveForm,
Patch(gblActiveForm,
Patch(
'My forms table',
LookUp(
'My forms table',
'Form ID' = gblActiveForm.'Form ID'
),
ctxUpdates
)
)
)
,
Set(gblActiveForm,
Patch(
gblActiveForm,
ctxUpdates
)
);
)
Ok, that looks complicated. What is really happening. I’m going to have a more detailed look at the follwowing lines of code.
Set(gblActiveForm,
Patch(gblActiveForm,
Patch(
'My forms table',
LookUp(
'My forms table',
'Form ID' = gblActiveForm.'Form ID'
),
ctxUpdates
)
)
)
To Patch the database with out updates we can use a simple Patch update. Where the first parameter is my table. The second parameter (the Lookup) gets us the record that we want to update. and finally we have a context variable containing the updates that we want to make.
Patch(
'My forms table',
LookUp(
'My forms table',
'Form ID' = gblActiveForm.'Form ID'
),
ctxUpdates
)
The above Patch will return the object as known within out database, after we have made an update. But remember that this will not include the updates that we made while we were offline. So we will need to merge the gblActiveForm with the output returned by the above Patch.
To merge two objects we can use the Patch function again. The syntax form this is shown below.
Set(Object1, Patch(Object1, Object2))
Once I made use of the above merge ability of the Patch function my forms were working as expected, even when the user went offline and online.
Now in our case Object2 is returned by the inner Patch function. Nesting Patch functions to merge an update with an existing record is quite a nice little pattern to remember when working with online/offline data..
Discover more from SharePains
Subscribe to get the latest posts sent to your email.