Reputation: 29240
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
Upvotes: 2
Views: 6084
Reputation: 18895
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.
Upvotes: 1
Reputation: 2258
You can find answer in this blog post. There described two ways:
- RetrieveMultipleRequest with LinkEntity class;
- Fetch
Upvotes: 0