In this #PowerShot, I will show you how to work with polymorphic data in canvas apps in Power Apps. We will take a look at different types of operations on Polymorphic lookup type columns in Microsoft Dataverse.
If you are not familiar with the polymorphic lookup column, please refer to this article here.
To work with Polymorphic lookup columns, we will require "AsType" and "IsType" functions that can be found on the Microsoft documentation here.
Note: As Polymorphic lookup columns support relationships to different tables in the same column, to identify the table, you can use the Actual Name of the table. Also, make sure that the table should be added as a data source in the app.
Let's Get Started!
Case 1: Fetch Polymorphic LookUp Column
To fetch details from the column, we will use the AsType function to identify the table, and then you can select keys of the related table.
Example: The app shows all the notes and these notes can be related to multiple tables based on the notes and attachments feature. To get notes for a specific table (in this case, the table name is BytheDevsDocuments), you can pass the reference in the AsType function and then select the key you want to fetch or you can save whole item details in a variable or collection.
The expression used in the Screenshot:
AsType(Gallery1_1.Selected.Regarding,BytheDevsDocuments).Name
Explanation:
Here, Gallery1_1 is the gallery that shows all the notes so control reference is passed.
Regarding is the name of the Polymorphic LookUp column.
BytheDevsDocuments is the related table whose column we want to fetch.
Name is the column that we are trying to fetch.
Case 2: Filter on Polymorphic Column
Filtering details based on the polymorphic column table can be achieved using the IsType function.
Example: In this case, the Notes table is related to multiple tables based on the Notes and Attachment feature. We are trying to fetch only those notes which are related to the ByTheDevsDocuments table in Microsoft Dataverse.
The expression used in the Screenshot:
Filter(Notes,IsType(Regarding,BytheDevsDocuments))
Explanation:
Here, Notes is the name of the table that contains the Polymorphic LookUp Column.
Regarding is the name of the Polymorphic LookUp column.
BytheDevsDocuments is the related table whose column we want to fetch.
Note: The IsType function is not delegable, so if you have a requirement to filter on only one table, then you can create a View in Dataverse table, but if you want to filter the results based on an input in the app, then the filter condition will be added in the app itself.
Case 3: Add/Update Data in Polymorphic Column
Update operation to the data in the Polymorphic LookUp column can be performed using the Patch expression or other update operations. To get details about the function used in this example, you can refer here.
Example: In this case, we are showing the list of Notes from Microsoft Dataverse and the Update button changes the related polymorphic lookup column based on the dropdown selection.
The expression used in the Screenshot:
Patch(Notes,LookUp(Notes,Note = Gallery1_2.Selected.Note),{Regarding:LookUp(BytheDevsDocuments,BytheDevsDocuments = Dropdown1.Selected.BytheDevsDocuments)});Notify("Record Updated Succesfully",NotificationType.Success)
Explanation:
Here, Notes is the name of the table that contains the Polymorphic Lookup Column.
Gallery1_2 is the gallery that shows all the notes so control reference is passed
Regarding is the name of the Polymorphic LookUp column.
BytheDevsDocuments is the related table whose column we want to fetch.
Once the record is updated, it shows a success notification.
Setup in Action
In this post, we saw how to work with Polymorphic Column types and data from the Microsoft Dataverse in Canvas Apps in Power Apps. A similar setup can be implemented when you are working with columns that lookup to multiple columns and work with that data in Canvas Apps.
I hope you found this interesting and it helped you. Thank you for reading!