Dynamically Change View On Polymorphic/Multi Table

Introduction

In today's blog, I will walk you through the process to show specific Entity view for Multi Table/Polymorphic lookup field based on another field value.

Multi table lookup is to create a lookup field in Dynamics CRM which will point to multiple entities. More details can be found on the below link -

https://staturestack.wordpress.com/2021/07/15/multi-table-lookup-polymorphic-lookup-field-in-dynamics-365-ce/

I have a polymorphic lookup field that is pointing to Visit and Building Entity and let's say we have a requirement to show Visit Entity only when Full Time Employee field value is Yes and Building Entity only when Full Time Employee is No.

To achieve this requirement we can write below JS code and attach it on Onload of the form and Onchange of the Full Time Employee field

function onLoad(executionContext) {
    formContext = executionContext.getFormContext();
    var isFTE = formContext.getControl("msft_isfulltimeemployee").getValue();
    alert("Full Time Employee" + isFTE);
    if (isFTE == "Yes") {
        alert(1);
        formContext.getControl("new_polymorphicfieldid").setEntityTypes(['bc_visit']);
    } else {
        alert(2);
        formContext.getControl("new_polymorphicfieldid").setEntityTypes(['bc_building']);
    }
}

Once you have published your changes and when you go to your form and open record, you will see Polymorphic lookup field Entity Type is getting changed dynamically based on the Full Time Employee field value -

Dynamically Change View On Polymorphic/Multi Table

Hope this helps!

Next Recommended Reading How to Create Custom Lookup View in CRM