Monday, October 8, 2012

Retrieving Child Entity Columns Using Query Expression

Problem:
                Need to retrieve child entity attributes to be retrieved along the parent entity records. So that some of the child entity record should be displayed on the data grid.

Solution:
                As the problem state that we are going to get some of child entity attribute so that we can display more detail of the child entity along the parent records. As in my previous posts regarding the query expressions I am using the “account” as main entity mean account will be my parent entity and the contact will be my child entity.For this post I have retrieve contact business phone and the parent account, I have also added two column in the data grid control to show the telephone and the parent account name ( for this post I have rearranged the columns so that primary contact information are side by side as can be seen in the Image 1).

Image 1

Let us start with the code which I have added for the link entity to be retrieved.Code is listed in List 1, Here in code you can see that I have first created the LinkEntity object first and then I have set the column name which I want to get for the link entity (the child entity). Here you can see that I have used the “telephone1” and then “parentcustomerid”, these are the two attributes which will be displayed for the child entity as additional information for the child entity.
Next is how link the parent and child entity here you can see that link entity has properties like “LinkFromEntityName” which is used to get or set the logical name of the entity that you are linking from mean the parent entity (here “account is my parent entity).After setting the name of the parent entity next is to set name of the parent attribute which will be mapped to the child entity. Here you have to set the “LinkFromAttributeName” which is used to set the name of the parent entity attribute which will be mapped from.
Next is to set the “LinkToEntityName” which in my case is the “contact” entity, mean I want to link to the “contact” entity and then set the name of the attribute of the child entity in the “LinkToAttributeName” property. Here I have set the “contactid” as the mapped attribute in the child entity.Mean I want to map the “account” “primarycontactid” which is lookup to the contact entity to be mapped with the “contact” “contactid” so that I will get telephone1 and parentcustomerid of the contact which is the then lookup to the account entity.
 
List 1


Last property of the link entity object is to set the Alias of the entity which will then used to access the attribute values in the completed call back. Here I have set the “EntityAlias” to “child”.Then at the end just add the link entity object to the query expression “LinkEntities” collections.
After adding the link entity to the query expression next is how to get the value of the link entity attributes when call back function will be called. I have list only the piece of code which is used to get the values of the linked entity, which you can see in the List 2.Here you can see that I have used the “AliasedValue” type to be getting using the GetAttibuteValue of entity and then converted to the respective type. For the phone number as it is of type string so I have used the value to convert to the Tostring. But for the EntityReference type (PrimaryAccount is of type entity reference) so the value is converted to EntityReferce type as you can see in List 2.As “AliasedValue” is “Value is of “object” type so you have to convert it to respective type before using the using vaue.
List 2

Last piece of code is the xaml code which is used to bind the two new properties, telephone is simple string type so it will be easy to bind but the Primary account is of type EntityReference so we will use the Name property of “EntityReferece” for binding as you can see in the List 3. I have also appended “(Primary Contact)” with the header text of the columns so that you can distinguish between the attributes.
List 3


That is all about how to retrieve child entity attribute and mapped the linked attribute between parent and child entity. Hope you get starting point of how to use linked entity to get addition information regarding child entity.  

Downloads
You can download the source code of the sample from here.
and working CRM 2011 solution from here.

Go to Home

All and any comments / bugs / suggestions are welcomed!

No comments: