How to get the created record id and other details using Patch in Power Apps Canvas apps?

I am back with another blog on Power Apps and this time after quite a gap of almost a month and half. And since this a blog after some time, I am going to share something very helpful in your day to day encounter with canvas apps.

Recently I was conducting this Power Apps session and got an very interesting question? How to get the record id and other field details once a record is created using Patch?

Below is a sample patch statement to create a new account in DataVerse. While this example shows DataVerse, it is applicable to any data source.

Patch(Accounts,
{
    'Account Name':"Patch Account 1 from PowerApps",
    'Account Number':"xyz"
});

It work’s great! But what if we want to get the newly created account id? So many times we use a patch statement but we hardly think of this scenario. Let’s try to solve this problem and alter the code a bit.

Set(NewAccount,Patch(Accounts,
{
    'Account Name':"Patch Account 1 from PowerApps",
    'Account Number':"xyz"
}));

In the above code, I set the output of Patch to a global variable. So the newly created account details shall be stored inside the NewAccount variable.

But when I try to get the account id from the variable, unfortunately I don’t have it. Screenshot below for reference.

But why is that so? In the Patch we set only two fields of account table. And hence the returned variable is only aware of those two fields used in the operation. Now let’s do a slight modification to this code.

Set(NewAccount, Patch(Accounts,
Defaults(Accounts),
{
    'Account Name':"Patch Account 1 from PowerApps",
    'Account Number':"xyz"
}));

This time I used the Patch statement but in the second parameter I passed the schema of the table to return. So now Patch function know the fields to return once the created is created successfully in the database.

Set(NewAccount, Patch(Accounts,
Defaults(Accounts),
{
    'Account Name':"Patch Account 1 from PowerApps",
    'Account Number':"xyz"
}));

Notify("Account Id: " & NewAccount.Account)

And below is the output.

Get newly created record id in Patch function of Canvas Apps

As you can see, the account id is visible in the notification bar.

Very simple but certainly very useful.

You might also like the below posts

Hope this helped!

Debajit Dutta
Business Solutions MVP