Modify and delete multi-table lookups within Dynamics 365 CRM

By | August 16, 2022

In our previous blog, we looked at “how to create a lookup field with multiple tables”, where we created the Distributor lookup with Account, Contact and Lead tables on the custom table Retailer. In this blog, we will see how to add or remove tables from an existing multi-table lookup. For which we shall add the Appointment table and remove the Contact table from the Distributor lookup.

Scenarios:

  • Add a table to a multi-table lookup.

To add a table to an existing multi-table lookup, we need to define a new 1: N relationship between the table which we want to add to the lookup and the table on which the lookup is placed.

As per our example, we want to add the Appointment table to the Distributor lookup on the Retailer table. So, we will define the 1: N relationship from the Appointment table to the Retailer table.

Below is the C# code using which we can define the 1: N relationship programmatically which will add the Appointment table to the lookup.

public void AddRelationshipToExistingPolymorphic(IOrganizationService service, ITracingService trace)

{

var createRelationshipRequest = new CreateOneToManyRequest();

// Define the new relationship details.

var oneToManyRelationship = new OneToManyRelationshipMetadata

{

ReferencingEntity = "ikl_retailer",

ReferencedEntity = "appointment",

SchemaName = "ikl_retailer_appointment"

};

createRelationshipRequest.OneToManyRelationship = oneToManyRelationship;

// Define the existing lookup details.

createRelationshipRequest.Parameters["Lookup"] = new LookupAttributeMetadata()

{

SchemaName = "ikl_Distributor",

DisplayName = new Label("Distributor", 1033)

 

};

service.Execute(createRelationshipRequest);

}

From the below screenshot we can see the Distributor lookup allows us to select Appointment records as well, after adding the relationship to the same.

Multi-table lookups

  • Remove a table from the multi-table lookup.

To remove a table from an existing multi-table lookup, we need to remove the existing 1: N relationship.

As per our example, we want to remove the Contact table from the Distributor lookup on the Retailer table. So, we will remove the 1: N relationship between the Contact table and the Retailer table, for which we would need the relationship schema name.

As per our example, the unique relationship schema name for the 1: N relation defined between Contact and Retailer is “ikl_retailer_contact”.

Below is the C# code using which we can delete the 1: N relationship programmatically which will remove the Contact table from the lookup.

public void DeleteRelationship(IOrganizationService service, ITracingService trace)

{

var deleteRelationshipRequest = new DeleteRelationshipRequest

{

// Name of the relationship to be deleted

Name = "ikl_retailer_contact"

};

service.Execute(deleteRelationshipRequest);

}

From the below screenshot we can see the Contact table is no more available for selection in the Distributor lookup.

Multi-table lookups

  • Delete the multi-table lookup.

There could be a scenario where we would want to delete the multi-table lookup itself. For this, we need to write the below C# code that would delete the lookup itself.

public void DeleteAttribute(IOrganizationService service, ITracingService trace)

{

DeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest();

//Table name where the lookup attribute exists

deleteAttributeRequest.EntityLogicalName = "ikl_retailer";

//Existing lookup logicalname

deleteAttributeRequest.LogicalName = "ikl_distributor";

service.Execute(deleteAttributeRequest);

 

}

Conclusion:

Thus, we saw how we can further manipulate the multi-table lookup field programmatically.