CRM Crate

How to perform read & write operations with Polymorphic lookup in Canvas App?

We will learn and understand to perform read & write operations with Polymorphic lookup in Canvas App. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Power Platform.

perform read & write operations with Polymorphic lookup

What is Polymorphic Lookup?

Multi-table lookup or Polymorphic lookup type field allow a user to use a specific table that has multiple One-To-Many (1:M) relationships to other entities / tables in the environment. A single lookup type column can refer to multiple other tables. A lookup value submitted to the multi-table type column will be matched to a record in any of the related tables.

Multi-table types are currently available in Microsoft Dataverse as static type attributes such as “Customer” lookup, which connects to Account and Contact. The new multi-table features gives users the power to define any other multi-table lookups they may need.


Reading or retrieving value from a Polymorphic Lookup

  • As name suggest, the Polymorphic Lookups will target more than one table / data source. Thus, we will need to specify the table / entity before consuming a Polymorphic Lookup.
  • We will leverage the functions AsType and IsType for achieving our requirement.

IsType()

The IsType function tests whether a record reference refers to a specific table type. The function returns a Boolean TRUE or FALSE.

AsType ()

The AsType function treats a record reference as a specific table type, sometimes referred to as casting. You can use the result as if it were a record of the table and again use the Record. Field notation to access all of the fields of that record. An error occurs if the reference isn’t of the specific type.

Understanding the scenario of demonstration

  • In this scenario, we have a Dataverse table named “Subscriber” which contains a Polymorphic Lookup field “Polymorphic Lookup” targeting two tables namely “Deliverables” and “Marketing & Campaigns“.
perform read & write operations with Polymorphic lookup
  • We will first allow the user to filter the Subscriber table according to the “Name” by passing an input parameter and further fetching the Polymorphic Lookup value for the given Subscriber row or record.
perform read & write operations with Polymorphic lookup
  • Considering the Power FX formula, we will initially retrieve the complete data of our polymorphic lookup field on a button click and store it in a variable. Below is the Power FX formula for performing this operation.
Component TypeComponent NameComponent Usage
TextInputTextInputUsed to take an input value from the user for filtering Subscriber table’s row/ record
TableSubscriberTable which contains a Polymorphic Lookup field
ColumnPolymorphic LookupPolymorphic Lookup field present in the Subscribe table
TableDeliverablesTarget table for the Polymorphic Lookup field
TableMarketing & CampaignsTarget table for the Polymorphic Lookup field

Set(
    varGetPLV,
    LookUp(
        Subscribers,
        Name = TextInput.Text
    ).'Polymorphic Lookup'
)
//Note: - We have stored the complete value in a variable named "varGetPLV".
  • Now, we will segregate the previously retrieved polymorphic data separately as per the target tables. In our case we have two target tables namely “Deliverables” and “Marketing & Campaigns“, thus we will use the “IsType” function to segregate the target tables and the “AsType” function to retrieve target table specific value from a Polymorphic Lookup. Below is the Power FX formula for performing this operation.
If(
    IsType(
        varGetPLV,
        'Marketing & Campaign'
    ),
    AsType(
        varGetPLV,
        'Marketing & Campaign'
    ).Name,
    AsType(
        varGetPLV,
        Deliverables
    ).Name
)
  • The output of the above Power FX formula will provide us the value stored in a Polymorphic Lookup field.

Updating or inserting value in a Polymorphic Lookup

  • As name suggest, the Polymorphic Lookups will target more than one table / data source. Thus, we will need to specify the table / entity & target row / record before inserting a value into a Polymorphic Lookup.

Understanding the scenario of demonstration

  • In this scenario, we have a Dataverse table named “Subscriber” which contains a Polymorphic Lookup field “Polymorphic Lookup” targeting two tables namely “Deliverables” and “Marketing & Campaigns“.
polymorphic lookup canvas app
  • We will first allow the user to select a new value for a Polymorphic Lookup, then allow the user to filter the Subscriber table according to the “Name” by passing an input parameter. Further we will insert the new value in the Polymorphic Lookup field of the filtered Subscriber row / record.
polymorphic lookup canvas app
  • Considering the Power FX formula, we will consume user inputs such as target table name, lookup value and target table’s row name. Further we will insert a new value in Polymorphic Lookup field on a button click as given in the below Power FX formula.
Component TypeComponent NameComponent Usage
TextInputTextInputUsed to take an input value from the user for filtering Subscriber table’s row/ record
DropdownDropdownToggleUsed to take an input value from the user for selecting the target table
DropdownDropdownDataUsed to take an input value from the user for selecting the lookup value
TableSubscriberTable which contains a Polymorphic Lookup field
ColumnPolymorphic LookupPolymorphic Lookup field present in the Subscribe table
TableDeliverablesTarget table for the Polymorphic Lookup field
TableMarketing & CampaignsTarget table for the Polymorphic Lookup field
If(
    DropdownToggle.SelectedText.Value = "Deliverables",
    Patch(
        Subscribers,
        LookUp(
            Subscribers,
            Name = TextInput.Text
        ),
        {
            'Polymorphic Lookup': LookUp(
                Deliverables,
                Name = DropdownData.SelectedText.Value
            )
        }
    ),
    Patch(
        Subscribers,
        LookUp(
            Subscribers,
            Name = TextInput.Text
        ),
        {
            'Polymorphic Lookup': LookUp(
                'Marketing & Campaign',
                Name = DropdownData.SelectedText.Value
            )
        }
    )
);
  • The output of the above Power FX formula will insert a new value in a Polymorphic Lookup field.

Validating our implementation in Canvas App

We will validate our implementation of reading & inserting value in a Polymorphic Lookup within a Canvas App as shown below.

polymorphic lookup canvas app

Thus, we learned to perform read & write operations with Polymorphic lookup in Canvas App.


5 1 vote
Article Rating
Subscribe
Notify of

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Necdet
2 months ago

Hi, great explanation. Can you also show how to filter Activities with Activity Type in Canvas App

Thanks in advance

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.