2

In my CRM database, I have two entity groups, A and B, linked by an m:n-relationship. There are many instances of entity A each linked to several instances of entity B. For a particular entity, let's say an an instance of entity A, how do I get all the linked instances of entity B?

var service = new OrganizationService(_connection);

var retrieveRequest = new RetrieveMultipleRequest();

retrieveRequest.Query = new QueryExpression
    {
        EntityName = "new_A",
        ColumnSet = new ColumnSet(true)
    }; 

var crmReponse = (RetrieveMultipleResponse)service.Execute(retrieveRequest); 

When viewing crmResponse in debugging mode, I see all instances of entity A, but I do not see anything within the RelatedInstances property. What am I doing wrong?

EDIT: After getting the first answer, I adapted my code as follows:

var linkEntities = new LinkEntity { 

    LinkFromEntityName = "new_A",
    LinkToEntityName = "new_B"

};

var retrieveRequest = new RetrieveMultipleRequest();

var query = new QueryExpression
{
    EntityName = "new_A",
    ColumnSet = new ColumnSet(true)
};

query.LinkEntities.Add(linkEntities);

retrieveRequest.Query = query;

var crmReponse = (RetrieveMultipleResponse)service.Execute(retrieveRequest); 

Now I get an error, though:

No system many-to-many relationship exists between new_customer_system_machine_type and new_customer_system_machine_check. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.

However, when I created that many-to-many-relationship, I did not specify any attribute names, it was merely between the entities. What am I doing wrong?

Also, my RetrieveMultipleRequest class does not have the property ReturnDynamicEntities

arik
  • 28,170
  • 36
  • 100
  • 156

2 Answers2

1

You need to specify the LinkFromAttributeName and the LinkToAttributeName. These are going to be the attributes of the from and to entities that contain the matching key values.

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • But the many-to-many-relationship did not generate any keys for neither of the entities. Additionally, when I specify a made-up attribute name, it throws an error because that attribute name does not exist. – arik Jul 13 '12 at 13:17
0

You can find answer in this blog post. There described two ways:
- RetrieveMultipleRequest with LinkEntity class;
- Fetch

paramosh
  • 2,258
  • 1
  • 15
  • 23