Answered by:
how to get related entity for N:N relationship

Question
-
I have a contact, and market entities.
they are N:N relationship with name list_association
contact entity doesn't have market id attribute, and market entity doesn't have contact id attribute.
if i know the contact, how my code get market list information
appreciate for any help
Thursday, July 9, 2009 5:20 PM
Answers
-
Hi, Yuhe.
Check this url . Try to use following code:
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "contact";
le.LinkFromAttributeName = "contactid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "entityid ";
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, July 9, 2009 7:34 PM
- Marked as answer by Yuhe Thursday, July 9, 2009 8:46 PM
Thursday, July 9, 2009 7:20 PMModerator -
Hi.
You can use AssociateEntitiesRequest agains Execute method of CRM WebService. Check this url .
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, July 9, 2009 7:33 PM
- Marked as answer by Yuhe Thursday, July 9, 2009 8:47 PM
Thursday, July 9, 2009 7:33 PMModerator
All replies
-
Hi, Yuhe.
When you create N:N relation between 2 entities - new link entity is created. When yoy associate two entities with N:N relation - record of this 'middle' type is created. This entity has two lookups. Check this url .
Truth is opened the prepared mind My blog - http://a33ik.blogspot.comThursday, July 9, 2009 5:46 PMModerator -
Yes, I saw the N:N relationship has a relationship entity: listmember
But in the customized entities, i can't find listmember.
how can i get the lookups?
thanksThursday, July 9, 2009 5:58 PM -
Hi, Yuhe.
When you create a relationship - you fill two fields - name of the relationship and name of link entity.
Truth is opened the prepared mind My blog - http://a33ik.blogspot.comThursday, July 9, 2009 6:28 PMModerator -
QueryExpression qe = new QueryExpression();
qe.EntityName = "contact";
qe.ColumnSet = new AllColumns();
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "contact";
le.LinkFromAttributeName = "contactid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "contactid";
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "list";
le2.LinkFromAttributeName = "listid";
le2.LinkToEntityName = "listmember";
le2.LinkToAttributeName = "listid";
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "contactid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { "093d7708-015a-de11-a9c7-00155dfad155" };
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
le.LinkEntities = new LinkEntity[] { le2 };
qe.LinkEntities = new LinkEntity[] { le };
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
it throw error: 'ListMember' entity doesn't contain attribute with Name = 'contactid'.
listmember is my releationship entity. and i don't know how to find what attributes listmember has since this entity is not in the customizes entities.
any help on this? thanksThursday, July 9, 2009 7:12 PM -
Hi, Yuhe.
Check this url . Try to use following code:
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "contact";
le.LinkFromAttributeName = "contactid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "entityid ";
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, July 9, 2009 7:34 PM
- Marked as answer by Yuhe Thursday, July 9, 2009 8:46 PM
Thursday, July 9, 2009 7:20 PMModerator -
yes. it works. thanks a lot.
Do you know how to add record for the N:N relationship?
ThanksThursday, July 9, 2009 7:32 PM -
Hi.
You can use AssociateEntitiesRequest agains Execute method of CRM WebService. Check this url .
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, July 9, 2009 7:33 PM
- Marked as answer by Yuhe Thursday, July 9, 2009 8:47 PM
Thursday, July 9, 2009 7:33 PMModerator -
For the retrieve, previous i use contact id get contact inforamtion, and it's no problem
but i try to use the contact id get the list information, then fails.
I only changed
qe.EntityName = "contact"; to qe.EntityName = "list";
then it says list doesn't have the contactid :((
i try to switch order of two LinkEntity, it doesn't work neither.
This one works:
QueryExpression qe = new QueryExpression();
qe.EntityName = "contact";
qe.ColumnSet = new AllColumns();
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "contact";
le.LinkFromAttributeName = "contactid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "entityid";
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "list";
le2.LinkFromAttributeName = "listid";
le2.LinkToEntityName = "listmember";
le2.LinkToAttributeName = "listid";
// The condition is to find the user ID.
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "entityid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { "093d7708-015a-de11-a9c7-00155dfad155" };
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
le.LinkEntities = new LinkEntity[] { le2 };
qe.LinkEntities = new LinkEntity[] { le };
// Execute the query.
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
and this one doesnt' work
QueryExpression qe = new QueryExpression();
qe.EntityName = "list";
qe.ColumnSet = new AllColumns();
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "contact";
le.LinkFromAttributeName = "contactid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "entityid";
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "list";
le2.LinkFromAttributeName = "listid";
le2.LinkToEntityName = "listmember";
le2.LinkToAttributeName = "listid";
// The condition is to find the user ID.
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "entityid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { "093d7708-015a-de11-a9c7-00155dfad155" };
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
le.LinkEntities = new LinkEntity[] { le2 };
qe.LinkEntities = new LinkEntity[] { le };
// Execute the query.
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
This one doesn't work neither:
QueryExpression qe = new QueryExpression();
qe.EntityName = "list";
qe.ColumnSet = new AllColumns();
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "list";
le.LinkFromAttributeName = "listid";
le.LinkToEntityName = "listmember";
le.LinkToAttributeName = "listid";
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "contact";
le2.LinkFromAttributeName = "contactid";
le2.LinkToEntityName = "listmember";
le2.LinkToAttributeName = "entityid";
// The condition is to find the user ID.
ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "entityid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { "093d7708-015a-de11-a9c7-00155dfad155" };
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
le.LinkEntities = new LinkEntity[] { le2 };
qe.LinkEntities = new LinkEntity[] { le };
// Execute the query.
BusinessEntityCollection bec = service.RetrieveMultiple(qe);
Thursday, July 9, 2009 7:49 PM -
Try to use following:
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "listmember";
le2.LinkFromAttributeName = "entityid";
le2.LinkToEntityName = "contact";
le2.LinkToAttributeName = "contactid";
Truth is opened the prepared mind My blog - http://a33ik.blogspot.comThursday, July 9, 2009 7:54 PMModerator -
Gee............ you have a magic wander.
thanks a lot. i changed as you suggest and also changed
ce.AttributeName from entityid to contactid, then it works.
the retrieve relation looks like to be in order. From-Entity to Middle-Relation-Entity, then Middle-Relation-Entity to To-Entity
still don't understand why need change ce.AttributeName from entityid to contactid
when i retrieve contact, it need name as "entityid"
when i retrieve list, it need name as "contactid"
in fact, it means same thing and i gave the same value
Really appreciate your help.Thursday, July 9, 2009 8:08 PM