Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Working offline

(0) ShareShare
ReportReport
Posted on by 241

I'm exploring the capabilities of working offline and I'm looking for help with some issues.

 

I created a simple app that works with 2 entities, a 'group' entity that will contain a number of child 'element' entities. The app contains 3 screens, an online 'home' screen and 2 offline screens (gallery+edit form).

 

My approach so far has been the following.

 

  1. Home screen: contains a 'groups' dropdown. You can choose a 'group' from this control and then click a button to 'start'. Once you click start, the chosen 'group' is cached with a SaveData call and also stored in a local collection/variable. Then, all child 'elements' (belonging to the chosen group) are also cached. After that Navigate to a new screen.
  2. Gallery screen: simply contains a gallery of the elements cached earlier. Clicking an item will load an edit screen for the (cached) element.
  3. Edit screen: Contains an "Edit Form" 
    Now this is a tricky part. I have configured the 'Data source' for the edit form to the 'elements' entity so it will be correctly typed and the editform can easily be configured with the fields I want the user to be able to edit. However, I can not use 'SubmitForm' to make a save action -- because that requires a connection. Instead, I'm going to save the changes locally, using the following code: 
    If( Form7.Valid,
     // Add to separate collection so we know we should submit it later.
     Remove( dataToSave, Filter( dataToSave, editentity=editedItem.editentity ) );
     Collect( dataToSave, editedItem );
     Patch( dataToSave, editedItem, { amount: amount_DataCard2.Update, finished: finished_DataCard2.Update, when: when_DataCard2.Update } );
     SaveData( dataToSave, "dataToSave" );
     // Also update the local collection of edited items (we don't want to work on an old version of the item)
     Patch( editItems, editedItem, { amount: amount_DataCard2.Update, finished: finished_DataCard2.Update, when: when_DataCard2.Update } );
     SaveData( editItems, "editItems" );
     Navigate( ElementChoice, ScreenTransition.CoverRight );
    ,
     Notify("Validation errors", NotificationType.Error);
    );
  4. Finally, back in the home screen, I added a 'submit edits' button that executes the following code:
    If( Connection.Connected,
     Patch( editentities, dataToSave ); // submit to data source
     Clear( dataToSave );
     SaveData( dataToSave, "dataToSave" );
    ,
     Notify( "No connection!");
    );


Now, for the most part, this seems to work as I intended. However, there are some quirks:

  • In step 3, if you're working online, you can call SubmitForm. This will also handle the validation and update the UI. Is there any way I can make the UI update with validation errors? The validity check 'Form7.Valid' works, but does not update the user interface.
  • In step 3, I'm now referencing the indivial 'datacard.Update' fields and patching these into the local variable to update it. This is quite cumbersome. Is there a way to retrieve the whole record directly from the editform? Or is it not advisable to work with the complete record?
  • I tested the app on a real device. After choosing a group in the home screen and caching the data locally, put the device in flight mode. When I edited an 'element' after restarting the app (still offline!) I noticed that the 'finished' datacard lost its option information. The field is configured as a "Two Options" Data type (No/Yes).
    How can I store the option/option set information locally so that this not lost if there's no connection?
  • Sometimes(?) a warning/error occurs during the patch call in step 4, saying something along the lines of 'Created On' is a read-only field. Should I be more selective as to what field updates I submit?

 

Sorry for the wall of text, but thanks in advance for any insights.

  • MrNappa Profile Picture
    241 on at
    Re: Working offline

    Sorry for the bump, but I'm wondering if anyone has some more information on the points I mentioned in my reply.

  • MrNappa Profile Picture
    241 on at
    Re: Working offline

    Hi Kris, thanks for the response.

     

    Regarding #1, would the following be a legit workaround? I tested it and it appears to be working, but I'm not sure it would work in edge cases...

     

    If( Form7.Valid
    , // <save code> , SubmitForm( Form7 ); );

    The 'SubmitForm' call would not submit anything because we know validation fails, but the validation errors will be shown correctly. (Is the validation always local?)

     

     


    @v-xida-msft wrote:

    For your third question, actually, it is an known issue with Two Option/Option Set type column. When you click the Two Option/Opton Set Dropdown box in your Edit form, it would send an HTTP call to CDS server to retrieve the available options, then populate them into the Two Option/Opton Set Dropdown box.

    If you are in Offline, it could not send an HTTP call to CDS Server to retrieve the available options, so the Two Option/Opton Set Dropdown box would not be populated with proper values.

    Do you know if a fix for this coming? If so, can I track this somewhere?
    I can work around this by using a number column (or even substitute option sets with a reference entity) but it would be inconvenient in a real world scenario.



    @v-xida-msft wrote:

    For your forth question, do you enable the "Created On" field within your Edit form? If you enabled the "Created On" field within your Edit form, please consider remove the  "Created On" field/data card from your edit form. Then modify your Patch formula as below:

    If(
    Connection.Connected, Patch(editentities, Defaults(editentities), dataToSave ); // submit to data source Clear( dataToSave ); SaveData( dataToSave, "dataToSave" ); , Notify( "No connection!"); );

    Please take a try with above solution, check if it could help in your scenario.


    Your suggested fix does not work, since 'dataToSave' is a table possibly containing multiple records. Not sure if it's possible to convert it to a 'ForAll' formula.

    The 'Created On' field is present in the entity but not enabled, I removed it from the edit form because it's read-only and not terribly interesting for the user of the app in this use case. 
    I did some more testing. The message regarding the 'Created On' column only manifests if I try to submit records that were restored in memory using a call to LoadData. Also, since the data patch fails the data I tried to edit is lost. Is there any way to handle errors during a Patch call?


    For reference, cleaned version of the (updated) save code so far:

    If( Form7.Valid,
     Remove( dataToSave, editedItem );
     Collect( dataToSave, editedItem ); // we need this to provide type to the 'dataToSave' collection
     Patch( dataToSave, editedItem, Form7.Updates );
     SaveData( dataToSave, "dataToSave" );
    
     Patch( editItems, editedItem, Form7.Updates );
     SaveData( editItems, "editItems" );
     Navigate( ElementChoice, ScreenTransition.CoverRight);
    ,
     SubmitForm(Form7);
    );

     

  • v-xida-msft Profile Picture
    on at
    Re: Working offline

    Hi @MrNappa ,

    For your first question, if you are in Offline, and you want to make the UI update with validation errors, I afraid that there is no way to achieve your needs in PowerApps currently.

    The validation errors would fire in your Edit form only when you execute the SubmitForm function. As an alternative solution, I think the If function could achieve your needs:

     

    If(
     IsBlank(DataCardValue1.Text),
     Notify("The Field1 could not be Blank!", NotificationType.Error),
     IsBlank(DataCardValue2.Text),
     Notify("The Field2 could not be Blank!", NotificationType.Error),
     IsBlank(DataCardValue3.Text),
     Notify("The Field3 could not be Blank!", NotificationType.Error),
     ...
     ...
     Remove(dataToSave, Filter( dataToSave, editentity=editedItem.editentity));
     Collect( dataToSave, editedItem ); ... SaveData( editItems, "editItems");
    Navigate( ElementChoice, ScreenTransition.CoverRight ); )

     

    For your second question, if you want to retrieve the whole record directly from the editform, please consider modify your Patch formula as below:

    Remove( dataToSave, Filter( dataToSave, editentity=editedItem.editentity ) );
    Collect( dataToSave, editedItem );
    Patch( dataToSave, editedItem, editform.Updates); /* <-- Modify formula here */
    SaveData(dataToSave, "dataToSave" );
    // Also update the local collection of edited items (we don't want to work on an old version of the item)
    Patch(editItems, editedItem, editform.Updates); /* <-- Modify formula here */
    SaveData( editItems, "editItems" );
    Navigate( ElementChoice, ScreenTransition.CoverRight );

    For your third question, actually, it is an known issue with Two Option/Option Set type column. When you click the Two Option/Opton Set Dropdown box in your Edit form, it would send an HTTP call to CDS server to retrieve the available options, then populate them into the Two Option/Opton Set Dropdown box.

    If you are in Offline, it could not send an HTTP call to CDS Server to retrieve the available options, so the Two Option/Opton Set Dropdown box would not be populated with proper values.

     

    For your forth question, do you enable the "Created On" field within your Edit form? If you enabled the "Created On" field within your Edit form, please consider remove the  "Created On" field/data card from your edit form. Then modify your Patch formula as below:

    If(
    Connection.Connected, Patch(editentities, Defaults(editentities), dataToSave ); // submit to data source Clear( dataToSave ); SaveData( dataToSave, "dataToSave" ); , Notify( "No connection!"); );

    Please take a try with above solution, check if it could help in your scenario.

     

    Best regards,

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,524 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,906 Most Valuable Professional

Leaderboard