top of page

Mastering GUID Comparisons with Polymorphic Fields in Canvas Apps

In the dynamic world of Power Apps, effectively retrieving and displaying data from lookup fields is essential, especially when dealing with polymorphic fields like the Dataverse Customer data type. One common challenge is comparing GUIDs with these polymorphic lookup fields to filter and display relevant data in a gallery. In this blog post, we'll dive into the nuances of this process, providing you with a detailed, step-by-step guide to enhance your Power Apps development skills.


Understanding Polymorphic Lookup Fields

Polymorphic lookup fields, such as the Customer data type in Dataverse, offer flexibility by allowing a single field to reference multiple entity types (e.g., Accounts or Contacts). This capability is powerful but requires a nuanced approach to data retrieval and comparison.


Error Message

Setting Up Your Data Source

Before diving into the comparison, ensure your Canvas App is connected to Dataverse and you have access to the relevant tables (e.g., Accounts, Contacts, and your main table containing the polymorphic lookup).

Steps to Connect to Dataverse:

  1. Open your Canvas App in Power Apps Studio.

  2. Click on Data in the left-hand panel.

  3. Add Data Source and select Dataverse.

  4. Choose the relevant tables (e.g., Accounts, Contacts, and your main table).


Creating a Gallery to Display Data

Add a Gallery:

  1. Insert a Gallery:

  • Go to the Insert tab.

  • Select Gallery and choose a layout (e.g., Vertical, Horizontal).

  1. Configure the Data Source:

  • Set the data source of the gallery to the main table containing the polymorphic lookup field.

  • Example: Gallery.Items = YourMainTable

Fetching Data from Polymorphic Lookup Fields

Using IsType and AsType for Polymorphic Fields in Canvas App

When working with polymorphic fields, you often need to determine the specific type of the field and cast it to the correct type. This is where the IsType and AsType functions come into play.


IsType Function

The IsType function checks whether a polymorphic lookup field is of a specific type. It returns a Boolean value (true or false). Syntax:

IsType(PolymorphicField, EntityType)

Example:

IsType(ThisItem.'Connected To', Accounts)

In this example, ThisItem.'Connected To' is the polymorphic lookup field, and Accounts is the entity type. The function checks if the lookup field references an Accounts entity.


AsType Function

The AsType function casts a polymorphic lookup field to a specific type, allowing you to access fields specific to that entity type.


Syntax:

AsType(PolymorphicField, EntityType)

Example:

AsType(ThisItem.'Connected To', Accounts).'Account Name'

In this example, ThisItem.'Connected To' is the polymorphic lookup field, and Accounts is the entity type. The function casts the lookup field to the Accounts type, allowing access to the 'Account Name' field.


Practical Example: Filtering Data in a Gallery

Scenario: Display Contacts Associated with a Specific Account


  1. Create a Dropdown to Select Account:

  • Insert a Dropdown control.

  • Set its items to the Accounts table.

  • Example: DropdownAccounts.Items = Accounts

  1. Filter the Gallery Based on Selected Account:

  • Set the gallery’s Items property to filter contacts based on the selected account.

  • Example:

Filter( 
	Contacts, IsType('Customer', Accounts) && 
	AsType('Customer', Accounts).AccId = DropdownAccounts.Selected.AccId
)

Testing and Validating

Run the App:
  • Test your app to ensure the gallery correctly displays data based on the GUID comparison.

Handle Edge Cases:
  • Ensure your app gracefully handles cases where the GUID does not match any records.

  • Example: Display a message like "No records found" when no data matches.


Best Practices

Optimize Performance:
  • Use delegation-friendly functions to handle large datasets efficiently.

  • Example: Use Filter instead of LookUp for better performance with large data sets.

User Experience:
  • Ensure the data displayed is relevant and easily accessible to users.

  • Example: Use sorting and search functionalities to enhance data navigation.

Security:
  • Implement appropriate security measures to protect sensitive data.

  • Example: Use role-based security to control data access.


Conclusion

Mastering the retrieval and comparison of GUIDs with polymorphic lookup fields in Canvas Apps opens up a realm of possibilities for creating dynamic and user-friendly applications. By following the steps outlined in this guide, you can effectively filter and display data, leveraging the power of Dataverse Customer data types to meet complex business needs.


Stay tuned for more insights and tutorials on Power Apps development. Happy app building!

Drop Us a Message, Share Your Thoughts

Thank You for Reaching Out!

© 2023 by InnovatePowerApps. All rights reserved.

bottom of page