The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM

CRM 4.0 Relationships Explained

CRM 4.0 Relationships Explained

  • Comments 26

Relationships in CRM 4.0 are quite powerful but hard to understand at first. There are so many moving parts tied to so many places that it is sometimes difficult to predict what the actual outcome is. So here is my attempt to explain a bit further what the relationships enhancements were in CRM 4.0. To start with let’s explain with a diagram what relationship types are available. Note that to determine the actual feasibility of a relationship (e.g. it may be the case that a specific relationship is not possible between an entity pair) you can use our APIs described here.

Relationships CRM 4.0

As you can see, the above gives you an enormous amount of flexibility on the types of relationships that you can model in CRM 4.0. Now let’s take a look at specific CRM 4.0 enhancements.

Backend

We performed a huge redesign of relationships in our backend. The major architectural changes were that we introduced the notion of many-to-many relationships and self referential relationships. To enable many-to-many relationships we implement intersect entities under the covers. To enable self referential relationships we added a couple of checks to prevent circular parental references and we also redid portions of several system relationships that were hardcoded to be metadata driven to enable system to system and multiple relationships.

UX

One of the biggest requests that we had for CRM 4 was to enable customization of the labels that get displayed as part of relationships; furthermore, sometimes relationship are only used as a logical construct for backend operations and aren’t meant to be displayed; hence the ability to “hide” portions of the UI related to relationships was also a requirement. To accomplish all the above we introduced several metadata attributes that control the display behaviors of relationships.

· This screencast walks you to the creation of a simple N:N relationship and describes how different pieces relate to the UI.

Programmability

Of course we had to provide means for programmers to take advantage of all the niceties that we implemented so we had to introduce a couple of new message and attributes and make some changes in the way fetchXml process relationships. Details are on the SDK but here are a couple of quick examples.

Creating new relationships

Yep, you can create brand new relationships (metadata) programmatically using the metadata API. Here is an example on how to create a Many-to-Many relationship. The CrmUtils class is just a wrapper class that a colleague created to create a label for only one language, 1033-English in this case (as you know CRM 4.0 is Multi Language enabled).

public static void createManyToManyTest(MetadataService metadataService)

{

ManyToManyMetadata manyToMany = new ManyToManyMetadata();

manyToMany.SchemaName = "new_relationship_name";

manyToMany.IntersectEntityName = "new_intersect_name";

//Side A

manyToMany.Entity1LogicalName = "account";

manyToMany.Entity1AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

manyToMany.Entity1AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

manyToMany.Entity1AssociatedMenuGroup = new CrmAssociatedMenuGroup();

manyToMany.Entity1AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

manyToMany.Entity1AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE A pointing to Side B", 1033);

manyToMany.Entity1AssociatedMenuOrder = new CrmNumber();

manyToMany.Entity1AssociatedMenuOrder.Value = 15001;

//Side B

manyToMany.Entity2LogicalName = "contact";

manyToMany.Entity2AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

manyToMany.Entity2AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

manyToMany.Entity2AssociatedMenuGroup = new CrmAssociatedMenuGroup();

manyToMany.Entity2AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

manyToMany.Entity2AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE B pointing to Side A", 1033);

manyToMany.Entity2AssociatedMenuOrder = new CrmNumber();

manyToMany.Entity2AssociatedMenuOrder.Value = 15001;

CreateManyToManyRequest manyToManyRequest = new CreateManyToManyRequest();

manyToManyRequest.IntersectEntitySchemaName = manyToMany.IntersectEntityName;

manyToManyRequest.ManyToManyRelationship = manyToMany;

metadataService.Execute(manyToManyRequest);

}

Adding/Removing records for a relationship

To add a new record to a many-to-many relationship you can use the following code. A similar code can be used to remove a record, just use DisassociateEntities message request/response instead of AssociateEntities.

Note that working with N:N relationships is slightly different than working with a One-to-many relationship (for the later you use SetRelated and RemoveRelated messages instead).

public static void addRelatedTest(TitanMiscTests.CrmSdk.CrmService service)

{

//Links (relates) an account record to a lead record in a manyToMany relationship

Moniker moniker1 = new Moniker();

moniker1.Name = "account";

moniker1.Id = new Guid("4BD77CC1-8D6B-DC11-B026-0017A41E8C1D");

Moniker moniker2 = new Moniker();

moniker2.Name = "lead";

moniker2.Id = new Guid("D1CAB380-C56B-DC11-B026-0017A41E8C1D");

AssociateEntitiesRequest request = new AssociateEntitiesRequest();

request.Moniker1 = moniker1;

request.Moniker2 = moniker2;

request.RelationshipName = "new_account_lead_custom";

service.Execute(request);

}

Retrieving relationships

The following fetch will retrieve all the leads associated with account with name “Foo” in the custom relationship whose intersect entity is “new_account_lead_custom”.

public static void retrieveEntitiesViaFetch(TitanMiscTests.CrmSdk.CrmService service)

{

string linkFetch = @"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""true"">

<entity name=""lead"">

<attribute name=""fullname""/>

<order attribute=""fullname"" descending=""true""/>

<link-entity name=""new_account_lead_custom"" from=""leadid"" to=""leadid"" visible=""false"" intersect=""true"">

<link-entity name=""account"" from=""accountid"" to=""accountid"" alias=""aa"">

<filter type=""and"">

<condition attribute=""name"" operator=""eq"" value=""Foo""/>

</filter>

</link-entity>

</link-entity>

</entity>

</fetch>";

string result = service.Fetch(linkFetch);

Console.WriteLine(result);

}

The same query can be accomplished using QueryExpression as follows, note how the query is constructed from bottom to top when compared with fetchXml.

public static void retrieveEntityListFromManyToMany(TitanMiscTests.CrmSdk.CrmService service)

{

//This code will retrieve a list of "leads" associated with the entity "Foo" on the relationship whose intersect entity is "new_account_lead_custom"

//Filter by the specific record that we are looking for

//(In this example we assume that there are no other accounts with the name Foo, otherwise

// if would be recommended to use the account "id" instead of the name.

ConditionExpression conditionName = new ConditionExpression();

conditionName.AttributeName = "name";

conditionName.Operator = ConditionOperator.Equal;

conditionName.Values = new object[1];

conditionName.Values[0] = "Foo";

FilterExpression selectByName = new FilterExpression();

selectByName.Conditions = new ConditionExpression[] { conditionName };

//Create nested link entity and apply filter criteria

LinkEntity nestedLinkEntity = new LinkEntity();

nestedLinkEntity.LinkToEntityName = "account";

nestedLinkEntity.LinkFromAttributeName = "accountid";

nestedLinkEntity.LinkToAttributeName = "accountid";

nestedLinkEntity.LinkCriteria = selectByName;

//Create the nested link entities

LinkEntity intersectEntity = new LinkEntity();

intersectEntity.LinkToEntityName = "new_account_lead_custom";

intersectEntity.LinkFromAttributeName = "leadid";

intersectEntity.LinkToAttributeName = "leadid";

intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };

//Create Query expression and set the entity type to lead

QueryExpression expression = new QueryExpression();

expression.EntityName = "lead";

expression.LinkEntities = new LinkEntity[] { intersectEntity };

RetrieveMultipleRequest request = new RetrieveMultipleRequest();

request.Query = expression;

//Execute and examine the response

RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

BusinessEntity[] entities=response.BusinessEntityCollection.BusinessEntities;

Console.WriteLine("Total related=" + entities.Length);

}

Cheers,

Humberto Lezama

  • I was reading this post to add some clarity to something I was working on.  It took me a moment to realize this post was about relationships and posted on Valentine's Day.  Nice touch guys.

  • So how would a retrieve be performed for a M:M relationship? From what I've seen so far an attribute is not created. Is there a new Retrieve message for this?

  • Hi, Many To Many relationships can be retrieved using linked entities.

    Specify the LinkFrom entity as the entity to be retrieved and the related entity as the LinkTo entity.

    In order to retrieve records ,you will need to specify the the query condition as "related entity's id column = related entity id value".

    Hope This Helps.

    Regards,

    Shubhi Bansal

    GrapeCity

  • Any chance we could get a full code example showing the retrieval of a N:N relationship?

  • How to add a provision for selecting values from a new entity on "To" attribute in Email Activity (Party List type)?

    i followed the below path and i tried to include the selection of new entity.

    Step 1. click Customization under Settings.

    Step 2. Cick on Customize Entities.

    Step 3. Double Click on Email.

    Step 4. Click on Attributes and double click on the "To" attribute.

    Step 5. it showing the Party List.

    In Step 5, How to add a provision for selecting values from a new entity on "To" attribute in Email Activity ?

    Regards,

    Israel G Pradeep

  • You have done an awesome job with this upgrade. The new CRM 4 is just about the perfect foundation for almost any system involving people and companies (ie 80% of the systems on the planet).

  • I was reading the post of Israel Pradeep "How to add a provision for selecting values from a new entity on "To" attribute in Email Activity (Party List type)?

    can you please let me know what happens in the step 5. as for me the Party list is in-active(greayed out)

    or is there any way if I can add my custom entity to the party list

    thanks

    Harvir

  • Hello, i just read this post and i would like to check if any one of you came across the following scenario in crm 4.o (since it works perfectly in crm 3.0):

    i have created a custom entity X.

    'Contact' entity has a 'one-to-many' relationship with 'X'

    'X' has a one-to-many relationship with 'Case'

    once published and u follow the scenario:

    contact-> X-> case, the lookup field in entity case that should contains the contact name displays the customer's name with the account icon. and upon saving of the case, you get the following error 'The requested record was not found or you do not have sufficient permissions to view it'.

    Anyone can help?

  • I don't see the option to add attributes to the entity that is between the M:M relationship as I guess this entity is hidden?

    Is there a way to do this?

    Thanks,

    Linna

  • hi, If I would like to retrieve an existing relationship in CRM without knowing GUID but the name of contact. is it possible to retrieve the entity?

  • I have a problem, I am making many to many relationship from one custom entity with One system entity, and on some condition if for example records are more then 10 it should not add, please let me know how can i do that.

    thanks

  • but I can't set the emails language. ,

  • Hi there,

    thanks for the article - helped with my many to many relationship but have the following issue outstanding;

    I have a Contact record with a 1 to many relationship with a custom entity.

    I have found many examples on how to programmatically add a link between two

    entities in a many-to-many relationship e.g.

    http://blogs.msdn.com/crm/archive/2008/02/14/crm-4-0-relationships-explained.aspx

    but not on how to acheive this with a one to many relationship. SetRelated

    didnt work because it returned with an error about Contact Entity not being

    able to use SetRelated

    http://msdn.microsoft.com/en-us/library/aa684058.aspx

    I have seen another post here on a similar issue but the resolution posted

    did not appear correct and did not work.

    Any help appreciated? links? blogs?

  • I'm trying to understand how to use Customer Relationships within the Lead, Contact, Opportunity to an Account, can anyone help?

    Also, is this the right flow; Lead, Contact, Opportunity to Account?

    Cmann

  • I am trying to create a relationship between Campaign Response and Leads, and I've added some fields to Campaign response to customize it.  When I create the relationship, and do a report using the report wizard, nothing displays.  So somehow the tables arent linking correctly.  I created the same relationship using a new crystal report, with the same results.  There are no real "fields" to link in those two entities from what I can see.  What am I missing here?

Page 1 of 2 (26 items) 12
Leave a Comment
  • Please add 3 and 2 and type the answer here:
  • Post