Entity Relationships
With CRM 4.0, the range of entity relationship options has been increased and you can now create the following:
| Additional CRM 4.0 Entity Relationships | Description |
| One-to-Many System-System | A system entity is linked to another system entity. The child system entity has a lookup field to associate it with a parent system entity. |
| Self-Referential | An entity linked to itself. For example a Case can be linked to a master Case. |
| Multiple Relationships Between Entities | For example the Account entity might have two relationships with a Contact entity - a primary and secondary Contact |
| Many-to-Many System-System, System-Custom & Custom-Custom | The one that everyone has been waiting for. Not only will this remove the need to build a “joining” entity, but you have control over how the relationships show up in the UI |
The customizations UI will only show entities that are valid for a given kind of relationship. For example, if an entity is not available for N:N relationships the “New N:N..” button won’t appear.
To determine the same programmatically, i.e. whether two entities are eligible to participate in a relationship, the CRM Metadata Web service exposes the following messages:
CanBeReferenced: Checks whether the specified entity can be the primary entity (one) in a one-to-many relationship.
CanBeReferencing: Checks whether the specified entity can be the referencing entity (many) in a one-to-many relationship.
CanManyToMany: Checks whether the entity can participate in a many-to-many relationship.
GetValidManytoMany: Returns the set of entities that can participate in a many-to-many relationship.
GetValidReferencedEntities: Returns the set of entities that are valid as the primary entity (one) from the specified entity in a one-to-many relationship.
GetValidReferencingEntities: Returns the set of entities that are valid as the related entity (many) to the specified entity in a one-to-many relationship.
Sample Code:
public static void relationshipsEligibilityTest(MetadataService metadataService)
{
GetValidManyToManyRequest getValidManyToManyRequest = new GetValidManyToManyRequest();
GetValidManyToManyResponse getValidManyToManyResponse = (GetValidManyToManyResponse) metadataService.Execute(getValidManyToManyRequest);
foreach (string nextName in getValidManyToManyResponse.EntityNames)
{
Console.WriteLine(nextName);
}
}





