Canvas Apps Embedded Canvas Apps Offline

How to add smart offline capabilities in embedded canvas apps

Airplane mode on a mobile device with Wi-Fi turned off
Airplane mode on a mobile device with Wi-Fi turned off

Introduction

If you read my previous post, you will know that there was an issue - the cached phone call details were showing up irrespective of which account I was looking at. We will see what needs to be done to prevent that from happening. So if you are creating a phone call record for Account A, and switch to Account B, the cached data will not show up. If you enter details for a phone call record for Account B and go back to Account A, you will see the cached data for the phone call that you were creating for Account A (and similarly for Account B).

The setup

To make this happen, we will change the properties that we had set in the previous post as following:

    1. OnVisible of the screen (or the OnEndTimer of a 1 second timer)
      LoadData(
          colPhoneCalls,
          "colPhoneCalls",
          true
      );
      ClearCollect(
          colPhoneCallsCurrentAccount,
          Filter(
              colPhoneCalls,
              Regarding.Account = [@ModelDrivenFormIntegration].Item.Account
          )
      );
      If(
          CountIf(colPhoneCallsCurrentAccount, true) = 0,
          UpdateContext({locCurrentAccountCount: 0}),
          UpdateContext({locCurrentAccountCount: 1})
      );
    2. In the OnChange and Value fields of each data card of the form that I want to load from cache, I am using the following code (using the Subject datacard as an example here)
      1. OnChange
        If(
            locCurrentAccountCount = 0,
            Collect(
                colPhoneCalls,
                {
                    Subject: Self.Value,
                    Regarding: [@ModelDrivenFormIntegration].Item
                }
            );
            UpdateContext({locCurrentAccountCount: 1}),
            Patch(
                colPhoneCalls,
                LookUp(
                    colPhoneCalls,
                    Regarding.Account = [@ModelDrivenFormIntegration].Item.Account
                ),
                {
                    Subject: Self.Value,
                    Regarding: [@ModelDrivenFormIntegration].Item
                }
            )
        );
        SaveData(
            colPhoneCalls,
            "colPhoneCalls"
        );
        
      2. Value
        If(
            locCurrentAccountCount = 1,
            LookUp(
                colPhoneCalls,
                Regarding.Account = [@ModelDrivenFormIntegration].Item.Account
            ).Subject,
            Parent.Default
        )
        
        

With this code, this is how the app behaves:

Smart offline in an embedded canvas app
Smart offline in an embedded canvas app

Conclusion

With a few simple changes, you can make the caching of data smarter to be associated with the account it was created for.

Recent articles

  1. How to add offline capabilities in embedded canvas apps
  2. A unique app to create and manage family trees – Part 2
  3. A unique app to create and manage family trees – Part 1

1 thought on “How to add smart offline capabilities in embedded canvas apps”

Leave a Reply