web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to get created rec...
Power Apps
Answered

How to get created record id from patch function ?

(1) ShareShare
ReportReport
Posted on by 149

I want to get record Id which is created by Patch function from PowerApps.

Is there any way to get that record Id so that I can use that Id to set in another record's lookup field

Categories:
I have the same question (0)
  • Verified answer
    timl Profile Picture
    36,391 Super User 2025 Season 2 on at

    Hi @rajmilansinh 

    The return value from Patch will include any server generated values. The code in the following post might be of interest to you...

     

    https://powerusers.microsoft.com/t5/General-Discussion/Retrieve-an-ID-from-SQL-table-after-patching-a-record-to-it/td-p/142441

     


     

    The return value from Patch includes any server generated identity values. If you did this for example,

    ClearCollect(NewCompany,
     Patch('[dbo].[tblCompany]',Defaults('[dbo].[tblCompany]'),{Name: TextInput1.Text})
    )

    ... you could use NewCompany.ID to retrieve the identity value that SQL Server generates.

     


  • kevinroy Profile Picture
    14 on at

    Several web sites recommend using ClearCollect to load the returned record.   I've had difficulty with this; I believe it's because ClearCollect creates a table, and accessing the ID for the newly-created record is not as straightforward as the following method.

     

    Instead, I have used UpdateContext with success.  For example:

     

    UpdateContext ({newRecord: Patch ('Table Name', Defaults('Table Name'), {Json key value pairs values to store in the item item})})

     

    Then, for example, "newRecord.ID" returns the ID for the new item and "newRecord.Title" returns the title.

    Of course, you can use Set instead, if you need the new record to be accessible across several screens.

  • JonSov Profile Picture
    4 on at

    How do you do this inside of a ForAll loop? You cant do clearcollect, and collect doesnt work for me because I need the value to reset every iteration of the loop.

  • kevinjroy Profile Picture
    2 on at

    First, clear a collection to contain the patched items:

    ClearCollect (patchedItems, {}); Clear (patchedItems)

     

    ForAll (collection of items to patch, 

      Collect (

          patchedItems,

          {aPatchedItem: Patch (....)}

       )

    )

    This gives you a full collection of patched items.   I'm not clear on why you need to reset a value for every iteration, but you perhaps can achieve your goal by inspecting the collection "patchedItems"

  • Baudelaire Profile Picture
    110 on at

    This is how I do it as well, for SharePoint that is.  If I know I only want the ID of the item (to pass to a flow for example, but the OP's use case is also valid), I do this:

    UpdateContext ({newRecord: Patch ('Table Name', Defaults('Table Name'), {Json key value pairs values to store in the item item}).ID})

     

    Now newRecord is just the item ID.

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard