locked
retrieve record through three connect entities(two N:N relationships) RRS feed

  • Question

  • Hi everyone,

    Please help me and I feel grateful for your replies!

    I am new to CRM and having difficulty on a plugin. The case is as following:

    There are three entities: E1, E2, E3

    E1 has N:N relationship with E2, E2 has N:N relationship with E3.

    there is a lookup field on E1 that connect E1 and E2

    I am writing a plugin on the form of E1. I need:

    when the user select an record "R1" (in E2) in the look up field on E1 form, I need to count the number of the records in E3 for that particular "R1" record.

    I am stuck now after using  " er_Id = (EntityReference)entity.Attributes["R1"];"

    I'm not sure how to retrieve the data from E2 and then further on getting the data from E3.

    I'm also planning on using the following code to get data from E3:

    "string E2 = “field2”;

    string E3 = “field3”;

    string relationshipEntityName = “name”;

    QueryExpression query = new QueryExpression(E2);

    query.ColumnSet = new ColumnSet(true);

    LinkEntity linkE2 = new LinkEntity(E2, relationshipEntityName, “field2”, “{E2 Primary field}“, JoinOperator.Inner);

    LinkEntity linkE3 = new LinkEntity(relationshipEntityName, E3, “field3”, “{E3 Primary field}“, JoinOperator.Inner);

    linkE2.LinkEntities.Add(linkE3);

    query.LinkEntities.Add(linkE2);"

    Thank you so much in advance!

    Thursday, August 13, 2015 2:31 PM