Reassign a Dataverse record owner within a canvas app

Recently I had a scenario where I needed to create a new record in a Dataverse table using a canvas app, but I then needed to have that record assigned to another user so that they as a basic user would see it. In… READ MORE [https://lewisdoes.dev/blog/reassign-a-dataverse-record-owner-within-a-
orange pumpkins on a field
Photo by Ekaterina Belinskaya on Pexels.com
In: Low Code Lewis Content 🚀

Recently I had a scenario where I needed to create a new record in a Dataverse table using a canvas app, but I then needed to have that record assigned to another user so that they as a basic user would see it. In this blog post we’ll take a look at how to assign a Dataverse record to someone else using the Patch() function in a canvas app.

Getting the record to update the owner of

So first, we’ll look at the basics. I’m going to start by attempting to retrieve a record in my app which I’ll update the owner of with the Patch() function in a moment. In my case I’m going to try to get hold of a record in my Accounts table which has the account name “Microsoft Corporation”.

To do this we’ll use a LookUp() function in our app. I’m simply going to use a text label to display a property from my retrieved record or ‘object’.

I’ll start by adding a text label to the screen and then I’ll use the following formula in the Text property.

LookUp(Accounts, 'Account Name' = "Microsoft Corporation").'Account Name'

Now you’ll see that I have the Name field of my retrieved record displayed on the screen.

Using this logic, we’ve been able to get the record with the following formula…

LookUp(Accounts, 'Account Name' = "Microsoft Corporation")

And we can then access the fields within the record using dot notation after the closing bracket at the end and referencing fields to get to the information we need. In this case of using a text label we wouldn’t be able to just reference the record without using dot notation to get into the fields as the text label wants a string value back but we would be able to use just the lookup without dot notation to get to a field when we need a record for something like a Patch(). We’ll use this in a moment when we use the Patch() function to do the owner update.

Getting a user record

So the next part we’ll take a look at, and see if we can do, is getting hold of the user record in Dataverse that we need to patch against the parent record which in my case is an Account. We’ll patch this to the Owner column which is a lookup against the User table.

I’m going to see if I can get hold of the user record for someone in my organsiation called John Doe. We’ll lookup John’s user record using his email address.

To get hold of John’s user record we’ll try a similar thing and display a property from the record using a text label. This time in the text property we’ll use the following formula to display John’s name.

LookUp(Users, 'Primary Email' = "johndoe@lewisdoes.dev").'Full Name'

Now I have John’s name displaying in the second text label I’ve added to my screen.

Now I know I can get hold of the record I need to update the owner of, and the record for the user that I want to patch against my parent record (Account). Let’s see if I can patch the record in.

Updating the owner of a record using a canvas app

Now for ease. I’m going to use a simple button in my app, I’m going to set a global variable with the record I want to update as my owner (User record), and I’ll just reference the global variable when I want to do my patch further down in my formula. I’ll also set a global variable for the record I want to patch and reference this in my Patch.

In the OnSelect of my button, I’ll use this formula to update the owner of my account record.

Set(
    gblAccountToPatch,
    LookUp(
        Accounts,
        'Account Name' = "Microsoft Corporation"
    )
);
Set(
    gblUserToSetAsOwner,
    LookUp(
        Users,
        'Primary Email' = "johndoe@lewisdoes.dev"
    )
);
Patch(
    Accounts,
    gblAccountToPatch,
    {Owner: gblUserToSetAsOwner}
);

Now if I select my button and check my record after, I can see that John is now the new owner of the record!

Now something to note with patching an owner column is that this is a polymorphic data type which effectively means that the lookup column can be patched with records from more than one table. In the case of patching Owners, the Patch() function already knows that this column can be patched with either a user record or a team record.

In other cases we might have to do some more work sometimes when working with polymorphic data types, which you can read more about here.

Understand record references and polymorphic lookups in canvas apps – Power Apps | Microsoft Learn

Written by
Lewis Baybutt
Microsoft Business Applications MVP • Power Platform Consultant • Blogger • Community Contributor • #CommunityRocks • #SharingIsCaring
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to LewisDoesDev.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.