Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

QueryExpression using AddLink

(0) ShareShare
ReportReport
Posted on by 255

Hi

I'm new to CRM programming but need to query an entity to get certain values,
then use those values to pull data from a 2nd entity which will populate a dropdown list.


So... get all accountid's where marketid = "e694b716-1375-e611-80cf-ba308e764b3f" from market_account entity.

Then
Get all accounts (name & accountid) from "account" entity with the accountid's from above?

:( Thanks in advance

*This post is locked for comments

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: QueryExpression using AddLink

    Hi Stuie,

    You can use the link entity to provide the relationship between the account entity and the related entity. See sample code that I attached below. You can also use advanced find and download the FetchXml query and use that to get your results:

            private EntityCollection RetrieveAccounts(Guid accountId)
            {
                EntityCollection rc = new EntityCollection();
                QueryExpression query = new QueryExpression()
                {
                    EntityName = "account",
                    ColumnSet = new ColumnSet(new string[] { "accountid", "accountnumber", "name" }),
                    LinkEntities =
                    {
                       new LinkEntity()
                       {
                           LinkToEntityName = "market_account",
                           LinkToAttributeName = "accountid", // Name or record in N<->N relationship
                           LinkFromEntityName = "account",
                           LinkFromAttributeName = "accountid",
                           LinkCriteria =
                           {
                                Conditions =
                                {
                                    new ConditionExpression("accountid", ConditionOperator.Equal, accountId)
                                }
                           }
                       }
                    }
                };
    
                try
                {
                    rc = service.RetrieveMultiple(query);
                    return rc;
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in (RetrieveAccounts: " + accountId.ToString() + ").", ex);
                }
            }


    If you use the Fetch Xml to retrieve your results, you can use the following code:

    EntityCollection rc = OrganizationService.RetrieveMultiple(new FetchExpression(fetchXml.ToString()));

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,575 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans