Hello everyone and welcome to my blog. In today’s blog, I will show how you can typecast polymorphic lookup columns to specific table types to compare and get the value.
For typecasting polymorphic lookup using PowerFx formula, you can use the AsType function. The AsType function is very useful whenever you want to typecast a polymorphic lookup into specific type.
But what is a polymorphic lookup? Polymorphic lookups (sometimes loosely termed as multi-table column) are columns that have references to multiple tables. You will come across these type of columns if you are working with a CRM system. For example – In DataVerse below are the type of polymorphic lookps.
- Customer – May represent an account or contact record.
- Owner – May be either a User or Team record.
- Regarding – May represent any table record with associated activities like email, task, appointment etc.
So let’s see how we can work with polymorphic lookup columns of DataVerse using AsType and IsType functions.
In the formula below, I am printing the owner field value depending on where the owner is a user or a team record. Carefully check the usage of IsType function. It is important to use the IsType function to check the type of the record type before you use AsType to typecast it into specific table type.
If(
IsType(
ThisItem.Owner,
Users
),
AsType(
ThisItem.Owner,
Users
).'Full Name',
AsType(
ThisItem.Owner,
Teams
).'Team Name'
)
Below, I have provided another example. And this time to compare. Notice the usage of the AsType in the below formula for comparison. Here I have added a column to the Account datasource to get the number of emails related to each account.
AddColumns(
Accounts,
RelatedEmailCount,
CountRows(Filter(
'Email Messages',
AsType(
ThisRecord.Regarding,
Accounts
).Account = Account
))
)
Now I guess it’s pretty clear how to use AsType and IsType. Now the next question? Can I use AsType function to typecast an object to a specific datasource type?
Example – Let’s assume there is an object in the below format.
{ 'Account Name' : "Adventure Works", 'Account Number' : "34719"}
I want to typecast this object as an Item of Account datasource. If I use AsType function to typecast it into Account object, it will throw an error.
To typecast an object to a specific datasource type, you can use the below syntax
Set(
acc,
Accounts@{
'Account Name': "Adventure Works",
'Account Number': "3728"
}
)
To know more about this, you can read my post.
Hope this helped!
Debajit Dutta
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.