Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Multiple linked Entity query CRM

Posted on by

Hi Experts 

I am new to crm 

I have a team entity that is linked the equipment entity (equipment entity are a lookup field to the team), then under the team i have a calendar linked to it.

Team > Equipment (TeamLookup)> Calendar 

I have written a QueryExpression to retrive the calendarID linked to the equipment below 

Guid TeamId = this.inputTeam.Get(executionContext).Id;

QueryExpression query = new QueryExpression("team") { ColumnSet = new ColumnSet("teamid") };
LinkEntity linkedEquipment = query.AddLink("equipment", "teamid", "new_team");
LinkEntity linkedCalender = linkedEquipment.AddLink("calendar", "calendarid", "calendarid");
query.Criteria.AddCondition("pro_team", ConditionOperator.Equal, TeamId);
EntityCollection results = service.RetrieveMultiple(query);

if (results.Entities.Count > 0)
{
Entity Resource = results.Entities[0];
currentResourceId = Resource.GetAttributeValue<Guid>("calendarid");
}

return currentResourceId;


}

However the below doesn't return an calendarID I get new_Team field doesnot exits on the Team Entity error 

*This post is locked for comments

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Multiple linked Entity query CRM

    Hi,

    Please try using the code below.

                Guid TeamId = this.inputTeam.Get(executionContext).Id;
                QueryExpression query = new QueryExpression()
                {
                    EntityName = "new_equipment", //Replace with the logical name of the equipment entity
                    ColumnSet = new ColumnSet("new_team"), //Replace with the logical name of the team lookup field in equipment entity
                };
    
    
                var linkedTeam = new LinkEntity
                {
                    LinkFromEntityName = "new_equipment", //Replace with the logical name of the equipment entity
                    LinkToEntityName = "team", //Replace with the logical name of the team entity
                    LinkFromAttributeName = "new_team", //Replace with the logical name of the team lookup field in equipment entity
                    LinkToAttributeName = "teamid", //Replace with the logical name of team GUID in team entity
                    JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                    EntityAlias = "Team",
                    Columns = new ColumnSet("calendarid") //Replace with the logical name of the calendar lookup in team entity
                };
    
                linkedTeam.LinkCriteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, TeamId)); //Replace with the logical name of team GUID in team entity
                query.LinkEntities.Add(linkedTeam);
    
                EntityCollection results = service.RetrieveMultiple(query);
                if (results.Entities.Count > 0)
                {
                    Entity Resource = results.Entities[0];
                    currentResourceId = ((EntityReference)((Resource.Attributes["Team.calendarid"] as AliasedValue).Value)).Id; ////Replace with the logical name of the calendar lookup in team entity
                }
                return currentResourceId;

    Hope this helps.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Multiple linked Entity query CRM

    Hi,

    Below is an example of multiple levels of linked entities. Try and modify this to work with your entities.

    Replace role with team, systemuserroles with equipment and systemuser with calendar.

           public bool IsUserInRole(Guid userId, Guid roleId)

           {

               LinkEntity systemUserLink = new LinkEntity()

               {

                   LinkFromEntityName = "systermuserroles",

                   LinkFromAttributeName = "systemuserid",

                   LinkToEntityName = "systemuser",

                   LinkToAttributeName = "systemuserid",

                   LinkCriteria =

                   {

                       Conditions =

                       {

                           new ConditionExpression("systemuserid", ConditionOperator.Equal, userId)

                       }

                   }

               };

               QueryExpression query = new QueryExpression()

               {

                   EntityName = "role",

                   ColumnSet = new ColumnSet("roleid"),

                   LinkEntities =

                   {

                       new LinkEntity()

                       {

                           LinkFromEntityName = "role",

                           LinkFromAttributeName = "roleid",

                           LinkToEntityName = "systemuserroles",

                           LinkToAttributeName = "roleid",

                           LinkEntities = {systemUserLink}

                       }

                   },

                   Criteria =

                   {

                       Conditions =

                       {

                           new ConditionExpression("roleid", ConditionOperator.Equal, roleId)

                       }

                   }

               };

               EntityCollection results = OrganizationService.RetrieveMultiple(query);

               bool rc = (results.Entities.Count > 0);

               return rc;

           }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans