Answered by:
How extract data from Related Entities

Question
-
in crm 2 custom entities 1.Bikeplan(primary) 2.Installment(related)
retrieving related entity records for the Bike plan entity.
using below code but didnot know how to extract related entity data values from response object.
plz help how to extract related entity attributes data form response
ConditionExpression condtion=new ConditionExpression(); condtion.AttributeName="new_referencenumber"; condtion.Operator=ConditionOperator.Equal; condtion.Values.Add("BLF-01661"); QueryExpression query =new QueryExpression("new_installment"); query.ColumnSet.AddColumns("new_installmentid", "new_month", "new_period"); //create the relationship object Relationship relationship = new Relationship(); query.Criteria.AddCondition(condtion); // name of relationship between bikePlan & installments relationship.SchemaName = "new_new_bikeplan_new_installment_BikePlan"; RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection(); relatedEntity.Add(relationship, query); //create the retrieve request object RetrieveRequest request = new RetrieveRequest(); request.RelatedEntitiesQuery=relatedEntity; request.ColumnSet = new ColumnSet("new_bikeplanid");//new_bikeplan request.Target = new EntityReference { Id = Guid.Parse("052D8657-3F4A-E411-ABEB-00155D0A7529"), LogicalName = "new_bikeplan" };
//execute the request RetrieveResponse response = (RetrieveResponse)orgService.Execute(request); string month = response.Entity.RelatedEntities["new_new_bikeplan_new_installment_BikePlan"].Entities[0].Attributes["new_month"].ToString();
Muhammad Sohail
- Edited by sohail450 Friday, October 3, 2014 1:48 PM
Friday, October 3, 2014 12:23 PM
Answers
-
Hi Sohail,
You can get the values from Response In this way
// Here you can check collection count if (((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("new_new_bikeplan_new_installment_BikePlan")) && ((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("new_new_bikeplan_new_installment_BikePlan")].Entities.Count > 0) { DataCollection<Entity> InstallmentCollection = response.Entity.RelatedEntities[new Relationship("new_new_bikeplan_new_installment_BikePlan")].Entities; foreach (Entity installment in InstallmentCollection) { if(installment.contains("new_month")) { string Month = installment["new_month"].ToString(); } } }
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
- Edited by Mohammad Yusuf Ansari Saturday, October 11, 2014 5:55 AM
- Proposed as answer by Mohammad Yusuf Ansari Saturday, October 11, 2014 5:56 AM
- Marked as answer by sohail450 Monday, October 13, 2014 12:18 PM
Friday, October 10, 2014 2:24 PM
All replies
-
Hi Sohail,
Can you please explain in steps what you want to achieve?
Thanks,
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
Saturday, October 4, 2014 10:35 AM -
Hi Sohail,
If it's N:N relationship between two entities then use below code. In below code we have entity 1 new_mediationsession and intersection entity
new_new_mediationsession_new_mediator. Here rel.d is the id of entity 2. Please let me know if below solution doesn't help you.
QueryExpression query = new QueryExpression() { EntityName = "new_mediationsession", ColumnSet = new ColumnSet(true), LinkEntities = { new LinkEntity { LinkFromEntityName = "new_mediationsession", LinkFromAttributeName = "new_mediationsessionid", LinkToEntityName = "new_new_mediationsession_new_mediator", LinkToAttributeName = "new_mediationsessionid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression { AttributeName = "new_mediatorid", Operator = ConditionOperator.Equal, Values = {rel.Id} } } } } } }; EntityCollection ec = service.RetrieveMultiple(query); foreach (var entity in ec.Entities) { }
Abhishek
Sunday, October 5, 2014 12:53 PM -
hello,
http://elsdesire.blogspot.com.au/2013/03/how-to-retrieveget-nnmany-to-many.html?m=1
you could Also try to use fetchxml to do the same.
regards
Jithesh
Monday, October 6, 2014 5:49 AM -
Hi
Mohammad Yusuf Ansari
i have 1:M relationship from bike plan to installments
as in above code i have retrieve the installment related to Bike plan that is 12 records but how i get each record attribute data from response object.
RetrieveResponse response = (RetrieveResponse)orgService.Execute(request); string month = response.Entity.RelatedEntities["new_new_bikeplan_new_installment_BikePlan"].Entities[0].Attributes["new_month"].ToString();
for example i am using this but getting red line on second line of code in visual studio
Muhammad Sohail
- Edited by sohail450 Friday, October 10, 2014 12:47 PM
Friday, October 10, 2014 12:44 PM -
Hi Sohail,
You can get the values from Response In this way
// Here you can check collection count if (((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("new_new_bikeplan_new_installment_BikePlan")) && ((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("new_new_bikeplan_new_installment_BikePlan")].Entities.Count > 0) { DataCollection<Entity> InstallmentCollection = response.Entity.RelatedEntities[new Relationship("new_new_bikeplan_new_installment_BikePlan")].Entities; foreach (Entity installment in InstallmentCollection) { if(installment.contains("new_month")) { string Month = installment["new_month"].ToString(); } } }
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
- Edited by Mohammad Yusuf Ansari Saturday, October 11, 2014 5:55 AM
- Proposed as answer by Mohammad Yusuf Ansari Saturday, October 11, 2014 5:56 AM
- Marked as answer by sohail450 Monday, October 13, 2014 12:18 PM
Friday, October 10, 2014 2:24 PM