Multi-Entity/Table lookup in Dynamics 365 CRM

By | August 23, 2021

Introduction:

Recently, Microsoft introduced the multi-entity/table lookup field which was one of the most awaited features. Previously it was only available for the OOB customer lookup field; for example, the customer field has contact and account for selection in the customer lookup field. In this release of Dynamics 365 CRM, it is now possible to create a lookup field that has a multi-entity/table. For example, for Retailer custom entity, we want to assign a Distributor and Distributor is lookup up field. Here, we want to assign Distributor to either Account, Contact or Lead entity. Earlier, in such cases we would have created three lookup fields with these entities and show/hide lookup field based on other options set field. But with recent release, it is now possible to create lookup field with multiple entities. So here we have created only one lookup field with Account, Contact and Lead entity. However, currently it can be done only programmatically but in the future it will be added to Microsoft UI.

Now, to create multi-entity/table lookup field we have written the below C# code. Here, we have created a Distributor lookup field with Account/Contact/Lead entities.

OrganizationRequest multiLookupOrgReq = null;
OneToManyRelationshipMetadata accountMetedata = null;
OneToManyRelationshipMetadata contactMetedata = null;
OneToManyRelationshipMetadata leadMetedata = null;
OrganizationResponse organizationResponse = null;
try
{
//create request object
multiLookupOrgReq = new OrganizationRequest();
// give lookup attribute information
multiLookupOrgReq.Parameters["Lookup"] = new LookupAttributeMetadata()
{
SchemaName = "ikl_Distributor",
DisplayName = new Label("Distributor", 1033)
};
//request name
multiLookupOrgReq.RequestName = "CreatePolymorphicLookupAttribute";
//Create relationship objects
accountMetedata = new OneToManyRelationshipMetadata();
accountMetedata.ReferencedEntity = "account";
accountMetedata.ReferencingEntity = "ikl_retailer";
accountMetedata.SchemaName = "ikl_retailer_account";
contactMetedata = new OneToManyRelationshipMetadata();
contactMetedata.ReferencedEntity = "contact";
contactMetedata.ReferencingEntity = "ikl_retailer";
contactMetedata.SchemaName = "ikl_retailer_contact";
leadMetedata = new OneToManyRelationshipMetadata();
leadMetedata.ReferencedEntity = "lead";
leadMetedata.ReferencingEntity = "ikl_retailer";
leadMetedata.SchemaName = "ikl_retailer_lead";
multiLookupOrgReq.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
{
accountMetedata,contactMetedata,leadMetedata
};
//execute request
organizationResponse = _service.Execute(multiLookupOrgReq);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

And now when we execute the above code it will create lookup field with four entities as shown in the below screenshot:

Multi-Entity/Table lookup in Dynamics 365 CRM

Conclusion:  As illustrated above, by using multi-entity/table feature you can easily set value in the lookup field instead of creating multiple lookup fields.

User Adoption Module

4 thoughts on “Multi-Entity/Table lookup in Dynamics 365 CRM

  1. Riccardo Gregori

    Great article!
    But please remove that “catch-throw”… most users will copy paste your code an we’ll get that code smell in production somewhere.
    Better not to have any exception management at all, than that one.

    Do you know if it’s possible to extend the entity list after the field has been created?

    Best regards
    _neronotte

    1. Inogic

      Thanks for your response.
      There is no way to extend entity list after field has been created.

  2. Manohar Patil

    What is the reference entity for Distributor Lookup field while creating it?

    1. inogic

      This lookup field will be created programmatically with the reference of entities mentioned in the blog. For e.g. If you want to create multi entity lookup field on Opportunity entity and the lookup field should set/show data of Contact, Account and Lead then reference entities would be Contact, Account and Lead.

      Hope this helps!

Comments are closed.