Search code examples
powerapps

PowerApps SubmitForm to another datasource


I am trying to implement offline featues like Outlook. If offline it should place values from EditForm1 to an local collection (Outbox). Is it possible to Submit editform to different sources dependant on the Connection.Connected variable? I would then create a timer that Submits its back when connected each x seconds.


Solution

  • If I understand correctly, you would like to save what's currently in EditForm1 to a local collection and upon receiving internet connection again, you'd like to submit the form again.

    You'll want to use a condition to save data that's currently in the form if you're connected to the internet otherwise collect it into a temporary collection.

    If(ToggleIsConnected.Value,
        //If you are connected to the internet, then write the data to the database.
        SubmitForm(Form1),
    
        //If you are NOT connected to the internet, then write the data to a temporary collection to be written later.
        Collect(temporary,
            {id: Max(temporary,id)+1,
                column1: InputColumn1.Text,
                column2: InputColumn2.Text
            }
        );
        SaveData(temporary,"temporary")
    )
    

    You can use a Toggle to check the internet connectivity. In the event that you had lost connection, saved data locally, and regained connectivity, the toggle will trigger on and perform the OnCheck actions automatically:

    If(!IsEmpty(temporary),
        // If the connection returns and there are records to be written, 
        // then perform these actions:
        ForAll(temporary,
            // For each of the records in the temporary collection, 
            // save their data to a new row in the connected datasource.
    
            // If writing was successful, copy it to a collection called 'success' 
            // to identify it.
            Collect(success,
                {id: id,
                    Record:                     
                        Patch(datasource,Defaults(datasource),
                            {column1: column1,
                                column2: column2
                            }
                        )
                }
            )
        );
    
        If(IsEmpty(Errors(datasource)),
            // If there are no errors with the datasource, 
            // go ahead and clear the entire temporary collection since you're writing.
            Clear(temporary),
    
            // Otherwise if there is an error, remove only the records that were successful. 
            // Then clear the successful records.
            // Keep records that had an error so they could be attempted later.
            Remove(temporary,Filter(temporary,id exactin Filter(success,!IsBlank(Record)).id));
            Clear(success)
        )
    )
    

    Note that here, I'm using Patch() to save what was in the temporary collection. I am not able to use SubmitForm unless I populate a form.

    There's some more steps involved to implement this further. Please see my video on this topic for more granular details: https://www.youtube.com/watch?v=j30xOM5OmRE