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