Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

QueryExpression using AddLink

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 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

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans