locked
CRMOnline 2016: Query Expression to get the related entity (4-Level Down/Up) RRS feed

  • Question

  • We have a scenario where we need to retrieve all the USERS from the related entities(given in the below screenshot). below is the picture of entity levels that we need to get the data. Is it possible using Query Expression with the given relationship? if so, Any help would be great.


    Madhu M.


    • Edited by Madhu_M Thursday, November 17, 2016 12:13 AM Updated the screenshot
    Wednesday, November 16, 2016 10:42 PM

Answers

  • Thanks for reading this post. Seems its possible using QueryExpression, below is the snippet that worked in my case

    QueryExpression userquery = new QueryExpression("systemuser");
                userquery.NoLock = true;
                userquery.Distinct = true;
                userquery.ColumnSet = new ColumnSet("systemuserid", "fullname");
                userquery.Criteria.AddCondition("isdisabled", ConditionOperator.Equal, false);
    
                LinkEntity elink = new LinkEntity("systemuser", "new_employee", "new_employee", "new_employeeid", JoinOperator.Inner);
                userquery.LinkEntities.Add(elink);
    
                          
                LinkEntity celink = new LinkEntity("new_employee", "connection", "new_employeeid", "record2id", JoinOperator.Inner);
                elink.LinkEntities.Add(celink);
    
                LinkEntity crlink = new LinkEntity("connection", "connectionrole", "record2roleid", "connectionroleid", JoinOperator.Inner);
                crlink.LinkCriteria.Conditions.Add(new ConditionExpression("category", ConditionOperator.Equal, 206490001));
                celink.LinkEntities.Add(crlink);
    
                celink.LinkCriteria.AddFilter(LogicalOperator.And);
    
                LinkEntity olink = new LinkEntity("connection", "opportunity", "record1id", "opportunityid", JoinOperator.Inner);
                olink.LinkCriteria.Conditions.Add(new ConditionExpression("opportunityid", ConditionOperator.Equal, context.PrimaryEntityId));
                celink.LinkEntities.Add(olink);



    Madhu M.


    • Marked as answer by Madhu_M Thursday, November 17, 2016 1:14 AM
    • Edited by Madhu_M Thursday, November 17, 2016 3:42 PM updated the code
    Thursday, November 17, 2016 1:13 AM

All replies

  • Thanks for reading this post. Seems its possible using QueryExpression, below is the snippet that worked in my case

    QueryExpression userquery = new QueryExpression("systemuser");
                userquery.NoLock = true;
                userquery.Distinct = true;
                userquery.ColumnSet = new ColumnSet("systemuserid", "fullname");
                userquery.Criteria.AddCondition("isdisabled", ConditionOperator.Equal, false);
    
                LinkEntity elink = new LinkEntity("systemuser", "new_employee", "new_employee", "new_employeeid", JoinOperator.Inner);
                userquery.LinkEntities.Add(elink);
    
                          
                LinkEntity celink = new LinkEntity("new_employee", "connection", "new_employeeid", "record2id", JoinOperator.Inner);
                elink.LinkEntities.Add(celink);
    
                LinkEntity crlink = new LinkEntity("connection", "connectionrole", "record2roleid", "connectionroleid", JoinOperator.Inner);
                crlink.LinkCriteria.Conditions.Add(new ConditionExpression("category", ConditionOperator.Equal, 206490001));
                celink.LinkEntities.Add(crlink);
    
                celink.LinkCriteria.AddFilter(LogicalOperator.And);
    
                LinkEntity olink = new LinkEntity("connection", "opportunity", "record1id", "opportunityid", JoinOperator.Inner);
                olink.LinkCriteria.Conditions.Add(new ConditionExpression("opportunityid", ConditionOperator.Equal, context.PrimaryEntityId));
                celink.LinkEntities.Add(olink);



    Madhu M.


    • Marked as answer by Madhu_M Thursday, November 17, 2016 1:14 AM
    • Edited by Madhu_M Thursday, November 17, 2016 3:42 PM updated the code
    Thursday, November 17, 2016 1:13 AM
  • It can be done using fetchxml quite easily also.

    Regards Faisal

    Wednesday, November 23, 2016 1:45 PM
  • Even In FetchXML it will not allow to put conditions between different Entities.

    Madhu M.

    Wednesday, November 23, 2016 8:08 PM