Bhubaneswar, Odisha, India
+91-8328865778
support@logyscal.com

How to create Polymorphic or multi-entity lookup field in Microsoft DataVerse and Dynamics 365

How to create Polymorphic or multi-entity lookup field in Microsoft DataVerse and Dynamics 365

Previously Lookup fields are only connected to one entity and Customer data type points to either Contact or Account entity but there was no provision to create custom polymorphic lookup. Now Microsoft introduced polymorphic lookup for custom fields. Using user Interface we cannot create this type of field we have to use code or XRMToolBox Tool.

Its easy to create polymorphic lookup field using XRMToolBox. For Microsoft article check here.

But if you are interested to create using code then use the sample code below to create the polymorphic lookup field.

string ConnectionString = "AuthType = OAuth; " +
                  "AppId=11223389-12ee-4a9e-aaae-a2591f45987d; " +
                  "Username=username@domain.onmicrosoft.com; " +
                  "Password=pwd; " +
                  "RedirectUri=app://localhost;" +
                  "Url = https://orgname.crm.dynamics.com/;";
 
 
 CrmServiceClient svc = new CrmServiceClient(ConnectionString);
 
            if (svc.IsReady)
            {
                // Create PolymorphicLookupAttribute 
                // with mycustomtable custom entity / table
                // referencing case, contact and account entity
                var varOrgRequest = new OrganizationRequest();
 
                // specify the request name
                varOrgRequest.RequestName = "CreatePolymorphicLookupAttribute";
 
                // specify lookup attribute details
                varOrgRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
                {
                    SchemaName = "soft_custompolymorphiclookup",
                    DisplayName = new Label("Custom Polymorphic Lookup", 1033)
                };
 
                // referencing entity is our custom entity named my custom table
                // referenced entity is incident
                var oneToManyRelation1 = new OneToManyRelationshipMetadata();
                oneToManyRelation1.ReferencingEntity = "soft_mycustomtable";
                oneToManyRelation1.ReferencedEntity = "incident";
                oneToManyRelation1.SchemaName = "soft_mycustomtable_soft_incident";
 
                // referencing entity is our custom entity named my custom table
                // referenced entity is contact
                var oneToManyRelation2 = new OneToManyRelationshipMetadata();
                oneToManyRelation2.ReferencingEntity = "soft_mycustomtable";
                oneToManyRelation2.ReferencedEntity = "contact";
                oneToManyRelation2.SchemaName = "soft_mycustomtable_soft_contact";
 
 
                // referencing entity is our custom entity named my custom table
                // referenced entity is account
                var oneToManyRelation3 = new OneToManyRelationshipMetadata();
                oneToManyRelation3.ReferencingEntity = "soft_mycustomtable";
                oneToManyRelation3.ReferencedEntity = "account";
                oneToManyRelation3.SchemaName = "soft_mycustomtable_soft_account";
 
                // populate OneToManyRelationships parameter of CreatePolymorphicLookupAttribute request
                varOrgRequest.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
                {
                    oneToManyRelation1, oneToManyRelation2, oneToManyRelation3
                };
 
                // specify the existing solution name 
                varOrgRequest.Parameters["SolutionUniqueName"] = "SoftSolution";
 
                var response = svc.Execute(varOrgRequest);
            }

The above code will create a new polymorphic lookup in table .

After creation if you want to add more tables to the lookup field than use the below code.

var createOneToManyRelationshipRequest = new CreateOneToManyRequest();
 
// referencing entity is our custom entity named mycustomtable
// referenced entity is contact - add the entity to be added
var oneToManyRelationAdd = new OneToManyRelationshipMetadata();
oneToManyRelationAdd.ReferencingEntity = "soft_mycustomtable";
oneToManyRelationAdd.ReferencedEntity = "contact";
oneToManyRelationAdd.SchemaName = "soft_mycustomtable_soft_contact";
 
createOneToManyRelationshipRequest.OneToManyRelationship = oneToManyRelationAdd;
 
 
// specify lookup attribute details to which new relationship is to be added
createOneToManyRelationshipRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
{
    SchemaName = "soft_mypolymorphiclookup",
    DisplayName = new Label("My Polymorphic Lookup", 1033)
};
 
 
var createOneToManyRelationshipResponse = svc.Execute(createOneToManyRelationshipRequest);

To remove an existing relationship from the lookup use the below code.

var removeRelationShip = new DeleteRelationshipRequest();
// specify schema name of the relationship
deleteRelationShip.Name = "soft_mycustomtable_soft_contact";
svc.Execute(deleteRelationShip);

To delete the polymorphic lookup use below code.


DeleteAttributeRequest varDelRequest = new DeleteAttributeRequest();
 
// specify the entity name
varDelRequest.EntityLogicalName = "soft_mycustomtable";
 
// specify the schema name of the entity
varDelRequest.LogicalName = "soft_custommultitablelookup";
svc.Execute(varDelRequest);

Hope this help

You can enroll now !We are giving 30% discount on our Internship Program

Don’t miss the chance to participate in the upcoming Internship Program which will be done using Microsoft Dot Net Web Development Full Stack Technology. The new batch will be starting from May 20, 2024.  We will have most experienced trainers for you to successfully complete the internship with live project experience.

Why to choose Our Internship Program?

Industry-Relevant Projects
Tailored Assignments: We offer projects that align with your academic background and career aspirations.
Real-World Challenges: Tackle industry-specific problems and contribute to meaningful projects that make a difference.

Professional Mentorship
Guidance from Experts: Benefit from one-on-one mentorship from seasoned professionals in your field.
Career Development Workshops: Participate in workshops that focus on resume building, interview skills, and career planning.

Networking Opportunities
Connect with Industry Leaders: Build relationships with professionals and expand your professional network.
Peer Interaction: Collaborate with fellow interns and exchange ideas, fostering a supportive and collaborative environment.

Skill Enhancement
Hands-On Experience: Gain practical skills and learn new technologies through project-based learning.
Soft Skills Development: Enhance communication, teamwork, and problem-solving skills essential for career success.

Free Demo Class Available