cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

How to create the change record for Patch dynamically

Suppose we have 10 text boxes to edit 10 fields in my datasource.
I would like to patch only the values of the text boxes that was edited.
How can we make the change record dynamically?

Instead of always patching all the 10 fields. I would like to Patch only modified fields in a single patch.
Eg. If fld3 and fld5 were modified I want to use Patch ( DS, {fld3:txt3.text,fld5:txt5})
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Frequent Visitor

Re: How to create the change record for Patch dynamically

Hi @mdevaney 

 

Found the solution:

 

There is no need to create a Patch record with modified fields only. Just use a single PATCH with all the fields in the record. 

The PATCH function implementation is smart enough to apply the changes to the modified fields only.

 

I created an XLS DataSource, eg. myXLDS(Fld1,Fld2,Fld3).

Created a canvas app with 3 Textboxes in a Gallery and analysed the network traffic.

Following are the findings.

 

Every time a PATCH(DS, Gallery.Selected, {Fld1: Fld1Val .....}) is called an HTTP POST request is made with ONLY 

the changed fields (ie. in comparison with the base record) as payload, even if the ChangeRecord contains all the fields.

But most importantly, the corresponding HTTP Response payload will contain the whole record that was updated in the datasource, and the in-memory BaseRecord gets replaced with this (Gallery.Selected in this case).

 

1. Single Patch

Patch(dsXLS,gallery.Selected,
{Fld1:Fld1.Text},
{Fld2:Fld2.Text},
{Fld3:Fld3.Text})

Observation:

  Single HTTP POST with ONLY modified fields. ie. if only Fld2.Text differs from the BaseRecord, only this will be part of the body.

  Response Body: All the fields from the record that was updated in the Datasource.

  NOTE: No HTTP Post requests are made if there are no fields that differ.

 

2. Multiple Patches - Single Field (with values independent of BaseRecord)

UpdateContext({vFld1:tFld1.Text, vFld2:Value(tFld2.Text), vFld3:Value(tFld3.Text)} );
Patch(dsXLS,gallery.Selected,{Fld1:vFld1});
Patch(dsXLS,gallery.Selected,{Fld2:vFld2});
Patch(dsXLS,gallery.Selected,{Fld3:vFld3});

Observation:

  One HTTP POST per Patch call, if the ChangeRecord (Single field in this case) differs from the BaseRecord.

  Response Body: All the fields from the record that was updated in the Datasource will be returned for each HTTP POST.

  NOTE: HTTP POST requests were NOT made if there are no fields that differ. ie. if only tFld2.text was different, only one HTTP POST was made even though there were 3 separate Patch calls.

 

3. Multiple Patches (with values dependent on BaseRecord)

Patch(dsXLS,gallery.Selected, {Fld1:Fld1.Text});
Patch(dsXLS,gallery.Selected, {Fld2:Fld2.Text});
Patch(dsXLS,gallery.Selected, {Fld3:Fld3.Text});

Observation:   

AVOID THIS: Even though the Patch logic is the same as Case 2, since every time, the BaseRecord gets updated with all the field values from the HTTP Response, the subsequent Patch attempts will be made in comparison with new BaseRecord every time and the final result may not be as expected.

 

 

View solution in original post

5 REPLIES 5
Highlighted
Super User
Super User

Re: How to create the change record for Patch dynamically

@William_Jude 

I do not believe this is possible in PowerApps.  From what I can tell there is no way dynamically determine which fields should be updated within a single patch statement.  You must use SUBMIT or define every field in patch from the beginning.

 

The only alternative I can think of is less than ideal.  It would be to PATCH the data for each line immediately after it changes by putting code in the OnChange property.  Several PATCHes would still be executed but not all at the same time.

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Highlighted
Frequent Visitor

Re: How to create the change record for Patch dynamically

Thanks @mdevaney

The reason for the post was:

1. I am not using a Form Control and therefore SubmitForm is not an option. 

2. I am trying to avoid the "not so ideal" options of using Patch with ALL the fields everytime OR making multiple Patch requests.

Highlighted
Super User
Super User

Re: How to create the change record for Patch dynamically

@William_Jude 

To address your point @#2:  I do not believe the PATCH all fields approach can be avoided without calling PATCH multiple times.  A characteristic of collections is they must have the same columns in all places within the app.  For example, if you create a collection like this:

 

ClearCollect(
    myCollection,
    {
        col1: "value"
        col2: "value"
    }
);

 

And then attempt to drop column #2

 

ClearCollection(myCollection,DropColumns(myCollection,"col2))

 

The resulting collection would look like this

 

ClearCollect(
    myCollection,
    {
        col1: "value"
        col2: Blank()
    }
);

 

That is why I believe it cannot be done...

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Highlighted
Frequent Visitor

Re: How to create the change record for Patch dynamically

Hi @mdevaney 

I too have the same feeling that it is not possible with the current version of powerapps, but thought of putting it out there ... 

 

Regarding the DropColumns, AddColumns etc. 

The documentation says:

"For all these functions, the result is a new table with the transform applied. The original table isn't modified. You can't modify an existing table with a formula."

 

So, if you collect it in a new collection, it will have only one column.

ClearCollect(myNewCollection,DropColumns(myCollection,"col2"))

Unfortunately, we cannot use the DropColumns function for the purpose, as the ColumnName should be a string literal. Could have used it if it was possible to use a variable instead.

 

 

Highlighted
Frequent Visitor

Re: How to create the change record for Patch dynamically

Hi @mdevaney 

 

Found the solution:

 

There is no need to create a Patch record with modified fields only. Just use a single PATCH with all the fields in the record. 

The PATCH function implementation is smart enough to apply the changes to the modified fields only.

 

I created an XLS DataSource, eg. myXLDS(Fld1,Fld2,Fld3).

Created a canvas app with 3 Textboxes in a Gallery and analysed the network traffic.

Following are the findings.

 

Every time a PATCH(DS, Gallery.Selected, {Fld1: Fld1Val .....}) is called an HTTP POST request is made with ONLY 

the changed fields (ie. in comparison with the base record) as payload, even if the ChangeRecord contains all the fields.

But most importantly, the corresponding HTTP Response payload will contain the whole record that was updated in the datasource, and the in-memory BaseRecord gets replaced with this (Gallery.Selected in this case).

 

1. Single Patch

Patch(dsXLS,gallery.Selected,
{Fld1:Fld1.Text},
{Fld2:Fld2.Text},
{Fld3:Fld3.Text})

Observation:

  Single HTTP POST with ONLY modified fields. ie. if only Fld2.Text differs from the BaseRecord, only this will be part of the body.

  Response Body: All the fields from the record that was updated in the Datasource.

  NOTE: No HTTP Post requests are made if there are no fields that differ.

 

2. Multiple Patches - Single Field (with values independent of BaseRecord)

UpdateContext({vFld1:tFld1.Text, vFld2:Value(tFld2.Text), vFld3:Value(tFld3.Text)} );
Patch(dsXLS,gallery.Selected,{Fld1:vFld1});
Patch(dsXLS,gallery.Selected,{Fld2:vFld2});
Patch(dsXLS,gallery.Selected,{Fld3:vFld3});

Observation:

  One HTTP POST per Patch call, if the ChangeRecord (Single field in this case) differs from the BaseRecord.

  Response Body: All the fields from the record that was updated in the Datasource will be returned for each HTTP POST.

  NOTE: HTTP POST requests were NOT made if there are no fields that differ. ie. if only tFld2.text was different, only one HTTP POST was made even though there were 3 separate Patch calls.

 

3. Multiple Patches (with values dependent on BaseRecord)

Patch(dsXLS,gallery.Selected, {Fld1:Fld1.Text});
Patch(dsXLS,gallery.Selected, {Fld2:Fld2.Text});
Patch(dsXLS,gallery.Selected, {Fld3:Fld3.Text});

Observation:   

AVOID THIS: Even though the Patch logic is the same as Case 2, since every time, the BaseRecord gets updated with all the field values from the HTTP Response, the subsequent Patch attempts will be made in comparison with new BaseRecord every time and the final result may not be as expected.

 

 

View solution in original post

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (6,794)