cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate I
Advocate I

How do I Access the returned value from a patch function

Hi,

 

I'm looking for an example on how I can access the returned value from a patch function.

The reference here: Here advises

"The return value of Patch is the record that you modified or created. If you created a record, the return value may include properties that the data source generated automatically."

 

But how do i access this value?
Example, I am using SharePoint as a datasource. 

I'm creating a new list item using the formula below. 

Patch(SharePointList,Defaults(SharePointList),{Title:"Test",Blah:1234})

 

When an item is created in SharePoint it is given an auto-generated ID. I'm looking to access the returned value/record so I can use the given ID within my app without having to re-query the datasource.

 

Can anyone provide an example on how this can be acheived?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Champion
Community Champion

Re: How do I Access the returned value from a patch function

Hi @duffysp

The documentation is pretty poor in the way it explains it.

You need to do this:

ClearCollect(MyPatchedRecords, Patch(SharePointList,Defaults(SharePointList),{Title:"Test",Blah:1234}))

MyPatchedRecords will contain one record in case of a single record patch, or multiple records if the patch statement patches a set of records.

 

You can also use the above for the Remove/RemoveIf function; but rather oddly the PowerApps team have decided to return not the deleted record/records but all the remaining records of the data source after the Remove/RemoveIf.  However, due to delegation issues, only 500 records will be returned, so this is useless for medium/large data sources

View solution in original post

9 REPLIES 9
Highlighted
Community Champion
Community Champion

Re: How do I Access the returned value from a patch function

Hi @duffysp

The documentation is pretty poor in the way it explains it.

You need to do this:

ClearCollect(MyPatchedRecords, Patch(SharePointList,Defaults(SharePointList),{Title:"Test",Blah:1234}))

MyPatchedRecords will contain one record in case of a single record patch, or multiple records if the patch statement patches a set of records.

 

You can also use the above for the Remove/RemoveIf function; but rather oddly the PowerApps team have decided to return not the deleted record/records but all the remaining records of the data source after the Remove/RemoveIf.  However, due to delegation issues, only 500 records will be returned, so this is useless for medium/large data sources

View solution in original post

Highlighted
Advocate I
Advocate I

Re: How do I Access the returned value from a patch function

That worked a treat @Meneghino, thanks for the help.

 

Highlighted
Regular Visitor

Re: How do I Access the returned value from a patch function

So... what happens if the patch function fails for some reason (e.g the connection to the database fails). How does PowerApps manage this and how do I access the error message that is returned?

Highlighted
Community Champion
Community Champion

Re: How do I Access the returned value from a patch function

Hi @FBGLUCK

 

Two things:

1) MyPatchedRecords in the above example will be empty

2) You can use the Errors() function to retrieve any errors, see the documentation for details

 

PowerApps may also give feedback in the browser, but that is not a process that you control.

 

Hope this helps.

Highlighted
Kudo Kingpin
Kudo Kingpin

Re: How do I Access the returned value from a patch function

Thanks so much @Meneghino, this was a real life-saver.

I don't think I would have realized how this was supposed to work without your answer so kudos to you! 

Highlighted
Advocate III
Advocate III

Re: How do I Access the returned value from a patch function

can we get record Id which is created by Patch function here ? in Powerapps to set that id in another entity's lookup field by another patch statement ?

Highlighted
Advocate I
Advocate I

Re: How do I Access the returned value from a patch function

@rajmilansinh

 

If you include the Defaults(DataSource) in your patch statement, it will return the ID in the collection. 

 

So use this format (as noted above):

ClearCollect(MyPatchedRecords, Patch(SharePointList,Defaults(SharePointList),{Title:"Test",Blah:1234}))

 

Then you can access the collection to get your ID value. For example, my collection will only have one record so I will use First to get the record I want to pull the ID from.  

 

This is the code I used to set varID with my newly patched record's ID: 

UpdateContext({varID:First(MyPatchedRecords).ID})

Highlighted
Frequent Visitor

Re: How do I Access the returned value from a patch function

Hi @n8greene,

I need to utilize the ID immidiately after patching. The process is as follows:
- User fills in input and presses OK when finished
- The button "OK" patches to a SQL server and saves information in a collection like you described.
- Ideally, the press on OK also triggers a mail that sends a link that includes the ID to another user.

However, the values in the mail are empty - it seems like powerapps is too slow saving the information in the collection, so that the reference returns an empty value from the collection (I have checked the flow sending the email and the input is empty). I have tested to simply write the results from the collection in a textbox in my app and the values are shown here after patching - so I do not think my code is the problems.

Any idea/workaround to how I can manage to add this information to the mail immediatly after patching? 

Frequent Visitor

Re: How do I Access the returned value from a patch function

Alternatively, if you know you are going to patch a single record, consider using a context variable instead to capture the returned record and use that to get fields within the returned record. E.g.

UpdateContext({ReturnedRecord : Patch(SharePointList,Defaults(SharePointList),{Title:"Test",Blah:1234})});
// get SharePoint ID field within ReturnedRecord
Set(ResultID, ReturnedRecord.ID)

 

Helpful resources

Announcements
secondImage

Demo-Extravaganza 2020

Check out these cool Power Apps & vote on your favorite!

secondImage

Community Highlights

Check out whats happening in Power Apps

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 (10,488)