Query Intersect Entity And Its Attribute - Sample Code

Introduction

When we set up a system N:N relationship between two entities, an intersect entity is created internally which is not accessible directly. Let’s say we have N:N relationship between the case and workorder entity.

And if you try to see attributes of this entity, you will find the following attributes.

 

So, in case we want to query this entity, we need to query it based on the related entities. If you are looking for the sample code to query the intersect entity and get its attribute, this post will help you.

Requirement

Query the above intersect entity and check if there is any work order associated to the case. If yes, we want to get the workorderid.

Solution

As said we can’t query intersect entity directly, but we can query it using case or work order. In our case, we will be querying it with the case entity. We are going to use QueryExpression class here and will be using its AddLink method to get the data from the intersect entity. We can use the following code.

  1. //Create Query Expression.  
  2.                         QueryExpression query = new QueryExpression()  
  3.                         {  
  4.                             EntityName = "incident",  
  5.                             ColumnSet = new ColumnSet("title"),  
  6.                             LinkEntities =  
  7.                         {  
  8.                             new LinkEntity  
  9.                             {  
  10.                                 LinkToEntityName = "him_incident_msdyn_workorder"//name of the intersect entity  
  11.                                 LinkFromAttributeName="incidentid",   
  12.                                 LinkToAttributeName = "incidentid",  
  13.                                 Columns=new ColumnSet(new string[]{"msdyn_workorderid" }), //workorder id in interset entity  
  14.                                 EntityAlias="intersect"//add alias name to refer attribute easily   
  15.                                 LinkCriteria = new FilterExpression  
  16.                                 {  
  17.                                     FilterOperator = LogicalOperator.And,  
  18.                                     Conditions =  
  19.                                     {  
  20.                                         new ConditionExpression  
  21.                                         {  
  22.                                             AttributeName = "incidentid",  
  23.                                             Operator = ConditionOperator.Equal,  
  24.                                             Values = { caseid }  
  25.                                         }  
  26.                                     }  
  27.                                 }  
  28.                                    
  29.                             }  
  30.                         }  
  31.                         };  
  32.                        // Obtain results from the query expression.  
  33.                         EntityCollection results = service.RetrieveMultiple(query);  

Now, to get the workorderid, we need to use AliasedValue class here. For example, to get the first workorderid, we need to use the following code.

  1. Guid workorderid =new Guid(result.Entities[0].GetAttributeValue<AliasedValue>("intersect.msdyn_workorderid").Value.ToString());  

Hope it will help someone!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.