Multi-Table Lookup/Polymorphic Lookup Field in Dynamics 365 CE

Hi Everyone,

We all know that a lookup pointing to multiple tables was an ask from many customers. Although we have a Partylist field available on Email entity, we don’t have any control to create the same as a Custom Field.

Our voice has been heard by Microsoft and released Multi-Table Lookup in Dynamics 365. For now, creation of this lookup is allowed via C# code or Web API.

Here is the code and explanation for the same.

I have four entities named as Vehicle, Motorcycle, Trucks and Cars.

We need a lookup on Vehicle entity which hold information about all other three entities.

I did run the below code and it created a lookup on Vehicle entity where I can select the record from any of the three entities (Motorcycle, Trucks or Cars).

            OrganizationRequest orgRequest = new OrganizationRequest();

            orgRequest.RequestName = "CreatePolymorphicLookupAttribute";

            // specify lookup attribute details
            orgRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
            {
                SchemaName = "new_myfirstpolymorphiclookup",
                DisplayName = new Label("My First Polymorphic Lookup", 1033)
            };

            // Relationship 1
            var oneToManyRelation1 = new OneToManyRelationshipMetadata();
            oneToManyRelation1.ReferencingEntity = "new_vehicle";
            oneToManyRelation1.ReferencedEntity = "new_motorcycle";
            oneToManyRelation1.SchemaName = "new_vehicle_new_new_motorcycle";

            // Relationship 2
            var oneToManyRelation2 = new OneToManyRelationshipMetadata();
            oneToManyRelation2.ReferencingEntity = "new_vehicle";
            oneToManyRelation2.ReferencedEntity = "new_truck";
            oneToManyRelation2.SchemaName = "new_vehicle_new_new_truck";


            // Relationship 3
            var oneToManyRelation3 = new OneToManyRelationshipMetadata();
            oneToManyRelation3.ReferencingEntity = "new_vehicle";
            oneToManyRelation3.ReferencedEntity = "new_car";
            oneToManyRelation3.SchemaName = "new_vehicle_new_new_car";

            orgRequest.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
            {
                    oneToManyRelation1, oneToManyRelation2, oneToManyRelation3
            };

            // specify the existing solution name 
            orgRequest.Parameters["SolutionUniqueName"] = "MySolution";
            var response = crmServiceClient.Execute(orgRequest);

Hope this helps.


Happy 365’ing
Gopinath.

2 thoughts on “Multi-Table Lookup/Polymorphic Lookup Field in Dynamics 365 CE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s