Answered by:
1:N custom entities with referential relationship, how to retrieve related entities

Question
-
I have found some examples of using QueryExpression with LinkEntity and FilterExpression for N:N related entity retrieval. I am unable to use this with my 1:N relationship to obtain existing related entities. I capture a GUID that was passed into an Iframe from the primary entity.
I am receiving a "Server was unable to proces request" after calling the service.Execute() method with the query object. I assume something is not defined properly... , unfortunately after spending a day looking for "how to" and finding various ways to do similar tasks, I have yet to find one good explanation of 1:N entity retrieval using custom entities.
My thoughs:
Im using one LinkEntity defition since this is a 1:N, the examples had two but were for an N:N... then again another example used two because of another join (although it was for system entities).
Im using the entity names as obtained from the webservice, same with the attributes used to link to/from. Perhaps this is not correct for joins?
Perhaps my order of from/to is incorrect, my To is the parent entity, From is the Child entity, Link Attribute is the parent's id property for both To and From.
Perhaps I'm overcomplicating things, in SQL server I have noticed that a column was added in the entity table with the parent GUID. Should i just use a standard retrieve with a filter for that column? So many ways to try, wish there was a simple answer.
Help would be appreciated.Thursday, April 30, 2009 8:36 PM
Answers
-
To retreive Data From related entities you don't need to use link entities. Following code shows that:
BusinessEntityCollection results = null; string msgDetail = ""; GUID parentProjectId = passedintofunction; try { QueryExpression query = new QueryExpression(); //create condition for filter for join operation ConditionExpression filterCondition = new ConditionExpression(); filterCondition.AttributeName = "abc_custom_entityid"; filterCondition.Operator = ConditionOperator.Equal; filterCondition.Values = new Object[] { parentProjectId }; //create filter for join operation? FilterExpression filter = new FilterExpression(); filter.Conditions = new ConditionExpression[] { filterCondition }; filter.FilterOperator = LogicalOperator.And; query.EntityName = EntityName.abc_custom_entity_child.ToString(); query.ColumnSet = new AllColumns(); query.Criteria = filter; //results = curService.RetrieveMultiple(query); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); RetrieveMultipleResponse response = (RetrieveMultipleResponse) curService.Execute(request); results = response.BusinessEntityCollection; } catch (Exception ex) { throw new Exception("Error in GetRelatedEntities: "+ex.Message); } return results;
Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Sunday, May 3, 2009 4:52 PM
- Marked as answer by Atomik33 Monday, May 4, 2009 1:47 PM
Thursday, April 30, 2009 9:33 PMModerator
All replies
-
Sample code:
BusinessEntityCollection results = null;
string msgDetail = "";
GUID parentProjectId = passedintofunction;
try
{
QueryExpression query = new QueryExpression();
LinkEntity relEntity = new LinkEntity();
//create condition for filter for join operation
ConditionExpression filterCondition = new ConditionExpression();
filterCondition.AttributeName = "abc_custom_entityid";
filterCondition.Operator = ConditionOperator.Equal;
filterCondition.Values = new Object[] { parentProjectId };
//create filter for join operation?
FilterExpression joinFilter = new FilterExpression();
joinFilter.Conditions = new ConditionExpression[] { filterCondition };
//one to many link (many to many would require another link defined for the other direction)
relEntity.LinkToEntityName = EntityName.abc_custom_entity.ToString();
relEntity.LinkToAttributeName = "abc_custom_entityid";
relEntity.LinkFromAttributeName = "abc_custom_entityid";
relEntity.LinkFromEntityName = EntityName.abc_custom_entity_child.ToString();
relEntity.JoinOperator = JoinOperator.Inner;
relEntity.LinkCriteria = joinFilter;//do we define criteria for parent here?
query.EntityName = EntityName.abc_custom_entity_child.ToString();
query.ColumnSet = new AllColumns();
query.LinkEntities = new LinkEntity[] { relEntity };
//results = curService.RetrieveMultiple(query);
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
RetrieveMultipleResponse response = new RetrieveMultipleResponse();
response = (RetrieveMultipleResponse) curService.Execute(request);
results = response.BusinessEntityCollection;
}
catch (Exception ex)
{
throw new Exception("Error in GetRelatedEntities: "+ex.Message);
}
return results;Thursday, April 30, 2009 8:42 PM -
To retreive Data From related entities you don't need to use link entities. Following code shows that:
BusinessEntityCollection results = null; string msgDetail = ""; GUID parentProjectId = passedintofunction; try { QueryExpression query = new QueryExpression(); //create condition for filter for join operation ConditionExpression filterCondition = new ConditionExpression(); filterCondition.AttributeName = "abc_custom_entityid"; filterCondition.Operator = ConditionOperator.Equal; filterCondition.Values = new Object[] { parentProjectId }; //create filter for join operation? FilterExpression filter = new FilterExpression(); filter.Conditions = new ConditionExpression[] { filterCondition }; filter.FilterOperator = LogicalOperator.And; query.EntityName = EntityName.abc_custom_entity_child.ToString(); query.ColumnSet = new AllColumns(); query.Criteria = filter; //results = curService.RetrieveMultiple(query); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); RetrieveMultipleResponse response = (RetrieveMultipleResponse) curService.Execute(request); results = response.BusinessEntityCollection; } catch (Exception ex) { throw new Exception("Error in GetRelatedEntities: "+ex.Message); } return results;
Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com- Proposed as answer by Andrii ButenkoMVP, Moderator Sunday, May 3, 2009 4:52 PM
- Marked as answer by Atomik33 Monday, May 4, 2009 1:47 PM
Thursday, April 30, 2009 9:33 PMModerator -
Hi Andriy,
I have used the above code, But its throwing an error.
from the line: feApproval.Conditions = new ConditionExpression[] { ceApproval }; //**** ERROR
//Fetching the lookup: Approval Type id (entityApprovalType) from Approval (entity) in PaymentRequest(entity), here: (PaymentReuest to Approval has 1:N relationship "gms_approval_paymentrequestid")
//1st - Fetching the Approval id by using the relationship 1:N from PaymentRequest entity
QueryExpression qeApproval = new QueryExpression();
ConditionExpression ceApproval = new ConditionExpression();
ceApproval.AttributeName = "gms_approval_paymentrequestid";
ceApproval.Operator = ConditionOperator.Equal;
ceApproval.Values = new Object[] { paymentRequestID };
FilterExpression feApproval = new FilterExpression();
feApproval.Conditions = new ConditionExpression[] { ceApproval }; //**** ERROR
feApproval.FilterOperator = LogicalOperator.And;
qeApproval.EntityName = crmdevapp.EntityName.gms_approval.ToString();
qeApproval.ColumnSet = new AllColumns();
qeApproval.Criteria = feApproval;
RetrieveMultipleRequest requestApproval = new RetrieveMultipleRequest();
RetrieveMultipleResponse responseApproval = (RetrieveMultipleResponse)service.Execute(requestApproval);
BusinessEntityCollection bceApproval = responseApproval.BusinessEntityCollection ;//**** in the above highlighted line its throwing an error, is: Property or indexer 'Microsoft.crm.sdk.query.filterExpression.conditions' cannon be assigned to -- it is read only..
please let me know 1st how do I retrieve the values of Approval Entity using Payment Request and then
2nd From that- how do I retrieve the lookup:ApprovalType using Approval GUID or KAY.
appreciates your help.
Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".Thursday, May 19, 2011 1:25 AM -
Israel:
change your line to
feApproval.Conditions.Add(ceApproval );good luck!
Friday, August 12, 2011 10:51 PM