cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
colbyturybury
Helper V
Helper V

Patching attachments from an offline app

Can anyone help with this problem. I have a form with an attachment control for uploading photos to a sharepoint list. The user is assessing equipment in a facility and sometimes they have a terrible data connection. What happens is a connection check is made if it is connected then a SubmitForm runs. Otherwise the data is pushed into a collection where it can be viewed in an offline gallery. This data is stored via a SaveData command. Below is the code for what was mentioned above. 

If(
Connection.Connected,
SubmitForm(HydronicPiping);
Navigate(BrowseGallery1,ScreenTransition.None);,
Collect(
colMyOfflineRecords,
{
Title: datEquipID.Text,
BuildingName: datBuildingName.Selected.Value,
SelectDiscipline: datSelectDiscipline.Selected.Value,
BarcodeNumber: txtBarcode.Text,
IsBarcodeRequired : datIsThereABarcode.Selected.Value,
AssessmentSubmitter: User().FullName,
Photos: galHydronicPiping.AllItems.Image2_9,
WhenCreated: Now(),
AttachPhotos: colTakePhoto,
}
);
SaveData(colMyOfflineRecords, "myOfflineRecords");
Navigate(OfflineScreen, ScreenTransition.None);
);

 

When the user returns to a good connection they can access the offline gallery on a separate screen and tap a button that runs the below code.

 

ForAll(
colMyOfflineRecords,
Patch(
USD259_Equip_Assess_DB,
Defaults(USD259_Equip_Assess_DB),
{
Title: Title,
'Building Name': {Value: BuildingName},
'Assessment Submitter': {DisplayName: AssessmentSubmitter,
Claims:"i:0#.f|membership|" & Lower(User().Email),
Department:"",
Email:"",
JobTitle:"",
Picture:""},

'Select Discipline': {Value: SelectDiscipline},
'Select Mechanical Equipment Type': {Value: EquipType},
'Barcode Number': BarcodeNumber,
'Is there a barcode': {Value:IsBarcodeRequired},
Attachments: AttachPhotos
}
)
);

 

How do I get the photos that are in the attachment control that is loaded into a collection for oflfine storage uploaded with the rest of the data.

14 REPLIES 14
RandyHayes
Super User
Super User

@colbyturybury 

To start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

It is more efficient to use the function as intended and will provide better performance.

 

So your formula should be:

With({_usr: User()}),
  Patch(USD259_Equip_Assess_DB,
    ForAll(colMyOfflineRecords,
        {
          Title: Title,
          'Building Name': {Value: BuildingName},
          'Assessment Submitter': 
              {DisplayName: AssessmentSubmitter,
               Claims:"i:0#.f|membership|" & Lower(_usr.Email),
               Department:"",
               Email: _usr.Email,
               JobTitle:"",
               Picture:""
              },
          'Select Discipline': {Value: SelectDiscipline},
          'Select Mechanical Equipment Type': {Value: EquipType},
          'Barcode Number': BarcodeNumber,
          'Is there a barcode': {Value:IsBarcodeRequired}
       }
    )
  )
)

 

However - for the attachments, you will need to utilize a PowerAutomate flow to store your attachments.  You can ONLY submit attachments through a Form.  Patch will not work.  So, since you are performing a bulk patch to your datasource and not an individual submitform, you would not be able to do this.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
colbyturybury
Helper V
Helper V

Can you explain With({_usr: User()}),

Hi @RandyHayes

I have used the ForAll and Patch pattern that @colbyturybury has shared multiple times and I haven't compared that with the one you shared, but just wanted to clarify that it is a format that works. That being said, I prefer 

Patch(Table, collection)

In this pattern, only one data call is made whereas in the others, multiple data calls are made. 

 

Just thought I would share my 2 cents here. 

 

Thanks,

Hardit Bhatia

Is there a way to save all the content of a form including attachments to the local device to upload later

RandyHayes
Super User
Super User

@PowerAddict 

Yes, the ForAll used as it was in its backward way will work, it will just work inefficiently and performance will suffer from it.  ForAll is a function that is used to return a table.

The thing about Patch(source, collection) is actually identical to the concept of Patch(source, ForAll(...))  In BOTH cases, you are providing a table to Patch...this is highly efficient!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
RandyHayes
Super User
Super User

@colbyturybury 

The With statement is used to perform the User() function only once in the formula.  It is relatively expensive in terms of performance, so issuing it many times is a waste of time and will slow down the process.  Doing it only once is all that is needed.

 

Yes, you can store the attachments to the device and then later recall them to upload to a flow.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

When pasting your code in the With statement errors out.

colbyturybury
Helper V
Helper V

Is there a way to save the data to the device then reload it into a form so the user can sync when they have a data connection again?

@colbyturybury 

Sorry - typo in the formula.  Should be:

With({_usr: User()},
  Patch(USD259_Equip_Assess_DB,
    ForAll(colMyOfflineRecords,
        {
          Title: Title,
          'Building Name': {Value: BuildingName},
          'Assessment Submitter': 
              {DisplayName: AssessmentSubmitter,
               Claims:"i:0#.f|membership|" & Lower(_usr.Email),
               Department:"",
               Email: _usr.Email,
               JobTitle:"",
               Picture:""
              },
          'Select Discipline': {Value: SelectDiscipline},
          'Select Mechanical Equipment Type': {Value: EquipType},
          'Barcode Number': BarcodeNumber,
          'Is there a barcode': {Value:IsBarcodeRequired}
       }
    )
  )
)
_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (5,332)