web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans