cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
duffysp
Level: Powered On

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
Meneghino
Level 10

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

8 REPLIES 8
Meneghino
Level 10

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

duffysp
Level: Powered On

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

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

 

FBGLUCK
Level: Powered On

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?

Meneghino
Level 10

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.

PhilD
Level 8

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! 

rajmilansinh
Level: Powered On

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
n8greene
Level: Powered On

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})

tfarsund
Level: Powered On

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? 

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 53 members 4,674 guests
Please welcome our newest community members: