How to create variable of data source type in Power Apps Canvas apps

This one is really interesting and trust me, even if you are an experienced Power Apps developer, this may be something you learn today.

Say you are working with DataVerse and you want to declare a variable of type Account. In conventional DataVerse server side conventional programming, you can do something like this.

var account = new Entity("account");
// set properties of this object
account["name"] = "Demo account"

Well, in Power Apps if I ask you to do the same you might come up with something like this.

Set(account, {'Name' : "Demo Account"});

Well, the account object would be created here too. But there is a catch.

The variable ‘account‘ is of type record but is not of type Account. It is just a custom variable. What if I told you, you can actually declare variable objects of type Accounts, Contacts or for that matter any DataVerse table record?

Wondering how you do it? Well, below is the code which do exactly the same stuff.

Set(
    account,
    Accounts@{
        'Account Name': "Demo Account",
        'Account': GUID("cd37f432-66f9-e911-a813-000d3af02cd4"),
        'Account Number': "123456",
        'Industry' : 'Industry (Accounts)'.Accounting
    }
);

Observe the use of @ operator here. When I use the convention <DataSourceName>@ and then create a record object, the platform realize that I am trying to create an object of type of DataSource.

Actually it even validates your schema. For example, If I try to include a property which does not correspond to a column in DataVerse, it throws an error.

Wonderful isn’t it? And now comes the advantages. Now since this variable is of type Account, you can use this variable in your Patch statements. This is one of the benefits of many benefits of having a variable of type of specific datasource.

Patch(
    Accounts,
    account,
    {'Account Number': "123456778"}
)

The example shown here is with DataVerse data source. But you can do the same with Sharepoint data source records as well.

Hope this helped!

You will also like the below posts

Debajit Dutta
Business Solutions MVP

3 thoughts on “How to create variable of data source type in Power Apps Canvas apps”

    1. Debajit Dutta (Business Solutions MVP)

      Hi Matthew,
      Honestly it is serendipity. I got stuck with a requirement and then was trying out some syntaxes. And then this one didn’t give an error. So I tried to run this and it worked.

      -Debajit

Comments are closed.