Implement offline support for Canvas App in Dynamics 365 CRM

By | July 19, 2018

Introduction:

Recently, we had a requirement where customer wants use Dynamics365 CE using PowerApp in Offline mode.

Following are the steps to implement offline support for Power App to Dynamics365:

1. Create a default Canvas app by using Dynamics 365. You can refer to our previous blog here.

2. Insert a new Screen in the power app and design the page accordingly. This screen we have consider as Home Screen so move this screen to the top as shown in below screenshot:

Implement offline support for Canvas App in Dynamics365 CRM

We have designed the home screen as shown in below screenshot:

Implement offline support for Canvas App in Dynamics365 CRM

3. In Edit screen1, on “onselect property” of “IconAccept1” write the formula as:

If(Connection.Connected,

SubmitForm(EditForm1),

Collect(accountstobeCreated,{name: DataCardValue14.Text,address:DataCardValue16.Text,revenue:DataCardValue15.Text,description:DataCardValue13.Text,noofemployee:DataCardValue11.Text} );

SaveData(accountstobeCreated, “newAccountsInLocalStorage”));

Implement offline support for Canvas App in Dynamics365 CRM

The above formula will first check if connection is connected (i.e. if application is connected to internet), if true application will submit the form else create a collection name as “accountstobeCreated” and will store the value of fields.

SaveData function stores the collection in local database under name.

4. Now back to the home screen, on “onselect” property of “nexticon” write the formula as:

If(Connection.Connected,

ClearCollect(AccountCollection, Accounts);

SaveData(AccountCollection,”AccountInLocalStorage”),

LoadData(AccountCollection,”AccountInLocalStorage”, true));

LoadData(accountstobeCreated,”newAccountsInLocalStorage”, true);

Navigate(BrowseScreen1,ScreenTransition.Cover)

Implement offline support for Canvas App in Dynamics365 CRM

The above formula will first check if connection is connected (i.e. if application is connected to internet), if true application will clear and collect “AccountCollection” which will stores all the records from account entity and later will save the records in “AccountCollection” under the name “AccountInLocalStorage”.

If connection is not available, the application will LoadData from accountCollection and accountstobecreated.

LoadData function re-loads a collection by name that was previously saved with SaveData

5. On Item property of BrowseGallery1 write formula as:

AccountCollection

Implement offline support for Canvas App in Dynamics365 CRM

6. Now Add a Timer Control on BrowseGallery1 and set the following property of Timer:

Auto Start: true

Repeat: true

Duration: 30000

7. On “OnTimerEnd” property of Timer Control write the formula as:

If(Connection.Connected &&CountRows(accountstobeCreated)>0,

ForAll(accountstobeCreated,Patch(Accounts, Defaults(Accounts),{name:name,description:description}));

Clear(accountstobeCreated);

Refresh(Accounts);

ClearCollect(AccountCollection,Accounts);

SaveData(AccountCollection,”accountInLocalStorage”))

Implement offline support for Canvas App in Dynamics365 CRM

Whenever the timer ends the application will check if internet connection is available and records are present in “accountstobecreated” collection. If true,

Records will be created in CRM using patch function.

“accountstobecreated” collection will be cleared and created records will be saved in “AccountCollection”.

Conclusion:

Using simple steps above user can implement offline support for Canvas App in Dynamics365 CRM

Generate Your Own New Leads Now Within Dynamics 365 CRM with Maplytics