2

I need to create/define a Many to Many relationship between a Lead and a custom entity as well as between a Contact and a custom entity. I can't seem to find any code examples of what I want to do.

This needs to work in both CRM 4 and CRM 5.

Are there any disadvantages to doing two N:1 relationships instead of the N:N relationship?

Justin
  • 73
  • 2
  • 7
  • Whether there are any disadvantages depends on what the relationship is supposed to be about. Generally, n:m relationships are a bit more limited in that there is no way to record any additional data about the connection between two records. They are sometimes a little, but overall not a lot simpler in their usage. – TeaDrivenDev Jun 02 '11 at 22:16

1 Answers1

0

You can do N:N relationships via the UI by going to an entities N:N relationships. There are advantages to doing 2 N:1 relationships with an intermediate entity over doing a single N:N such as you can store relationship attributes (a role for the relationship), and also you can chain workflows through a double N:1 relationship.

EDIT:

To create N:N relationships through code you can refer to the CRM 2011 SDK help page "Create and Retrieve Entity Relationships", excerpted below. You will be able to do something similiar via the Metadata service in 4.0 -

Create an N:N Entity Relationship

The following sample uses a EligibleCreateManyToManyRelationship method to verify that the Account and Campaign entities can participate in a N:N entity relationship and then creates the entity relationship by using CreateManyToManyRequest.

bool accountEligibleParticipate =
    EligibleCreateManyToManyRelationship("account");
bool campaignEligibleParticipate =
    EligibleCreateManyToManyRelationship("campaign");

if (accountEligibleParticipate && campaignEligibleParticipate)
{

    CreateManyToManyRequest createManyToManyRelationshipRequest =
        new CreateManyToManyRequest
    {
        IntersectEntitySchemaName = "new_accounts_campaigns",
        ManyToManyRelationship = new ManyToManyRelationshipMetadata
        {
            SchemaName = "new_accounts_campaigns",
            Entity1LogicalName = "account",
            Entity1AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Account", 1033),
                Order = 10000
            },
            Entity2LogicalName = "campaign",
            Entity2AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Campaign", 1033),
                Order = 10000
            }
        }
    };

    CreateManyToManyResponse createManytoManyRelationshipResponse =
        (CreateManyToManyResponse)_serviceProxy.Execute(
        createManyToManyRelationshipRequest);


    _manyToManyRelationshipId =
        createManytoManyRelationshipResponse.ManyToManyRelationshipId;
    _manyToManyRelationshipName =
        createManyToManyRelationshipRequest.ManyToManyRelationship.SchemaName;

    Console.WriteLine(
        "The many-to-many relationship has been created between {0} and {1}.",
        "account", "campaign");
}

EligibleCreateManyToManyRelationship

The following sample creates a EligibleCreateManyToManyRelationship method that uses CanManyToManyRequest to verify whether an entity can participate in a N:N entity relationship.

/// <summary>
/// Determines whether the entity can participate in a many-to-many relationship.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns></returns>
public bool EligibleCreateManyToManyRelationship(string entity)
{
    CanManyToManyRequest canManyToManyRequest = new CanManyToManyRequest
    {
        EntityName = entity
    };

    CanManyToManyResponse canManyToManyResponse =
        (CanManyToManyResponse)_serviceProxy.Execute(canManyToManyRequest);

    if (!canManyToManyResponse.CanManyToMany)
    {
        Console.WriteLine(
            "Entity {0} can't participate in a many-to-many relationship.", 
            entity);
    }

    return canManyToManyResponse.CanManyToMany;
}
cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • I need to do it in code, C# specifically. I've been playing with creating the relationships via UI all morning trying to figure out if it's worth the effort of adding in N:N or just doing two N:1s. – Justin Jun 01 '11 at 19:18
  • Thanks Cole! It looks like we will be going with several N:1 relationships since we need to keep some information to display later. – Justin Jun 03 '11 at 13:29
  • No problem! Double N:1 is what I use for most instances. – cchamberlain Jun 03 '11 at 14:37