Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Answered

Get account and related information

Posted on by 55

Hello ,

I need to get an account ant some information from related entities .

it's ok  when i try to get n:1 relationship but when i try to add n:n relationship it's not working .my code :

            QueryExpression queryaccount = new QueryExpression();
            
            queryaccount.EntityName = "account";
            
            queryaccount.NoLock = true;


            queryaccount.ColumnSet = new ColumnSet(true);
            

            if (!string.IsNullOrEmpty(id))
                queryaccount.Criteria.AddCondition("crm_identifiant", ConditionOperator.Equal, id);




            var linkCarteClient = new LinkEntity("account", "crm_carte_abonnement", "accountid", "crm_compteid", JoinOperator.LeftOuter);
            linkCarteClient.EntityAlias = "carte";
            linkCarteClient.Columns = new ColumnSet(true);

            queryaccount.LinkEntities.Add(linkCarteClient);





            var linkAvoirAccount = new LinkEntity("account", "crm_avoir", "accountid", "crm_compteid", JoinOperator.LeftOuter);
            linkAvoirAccount.EntityAlias = "crm_avoir";
            linkAvoirAccount.Columns = new ColumnSet(true);
            queryaccount.LinkEntities.Add(linkAvoirAccount);



            LinkEntity le = new LinkEntity();
            le.LinkFromEntityName = "account";
            le.LinkFromAttributeName = "accountid";
            le.LinkToEntityName = "listmember";
            le.LinkToAttributeName = "entityid";

            LinkEntity le2 = new LinkEntity();
            le2.LinkFromEntityName = "listmember";
            le2.LinkFromAttributeName = "listid";
            le2.LinkToEntityName = "list";
            le2.LinkToAttributeName = "listid";

            


            /////////

            LinkEntity le3 = new LinkEntity();
            le3.LinkFromEntityName = "list";
            le3.LinkFromAttributeName = "listid";
            le3.LinkToEntityName = "campaignitem";
            le3.LinkToAttributeName = "entityid";

            LinkEntity le4 = new LinkEntity();
            le4.LinkFromEntityName = "campaignitem";
            le4.LinkFromAttributeName = "campaignid";
            le4.LinkToEntityName = "campaign";
            le4.LinkToAttributeName = "campaignid";

            le4.LinkEntities.Add(le3);
            
            le3.LinkEntities.Add(le2);
            le2.LinkEntities.Add(le);

          



            queryaccount.LinkEntities.Add(le);



            try
            {
                
                EntityCollection accounts = service.RetrieveMultiple(queryaccount);
                
                if (accounts.Entities != null && accounts.Entities.Count > 0)
                {
                  
                    Entity Account = accounts[0];
                    LogWebService.Log(ref _log, String.Format("Sortie de la fonction {0}", MethodBase.GetCurrentMethod()), LogWebService.LogLevel.Info);
                    return Account;

                }

how to get the information about the marketing campaign ?

Thank you

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get account and related information

    Hi Nando07,

    Or you can refer another link:

    Arpit's Dynamics 365 Blog: Retrieve N:N relationship records by Query Expression in MSCRM (arpitmscrmhunt.blogspot.com)

    Based on example,  you’ll receive information of  LinkEntity1 in the result EntityCollection.

    If you want to return campaign, you may add campaign entity to query.

    query.LinkEntities.Add(linkEntity4);

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Nando07 Profile Picture
    Nando07 55 on at
    RE: Get account and related information

    Hi Leah ,

    Thank you .

    now i got the correct query.

    but how i get into the information of the campaign entity?

                string entity1 = "account";
    
                string entity2 = "list";
    
                string relationshipEntityName = "listmember";  
    
                QueryExpression query = new QueryExpression(entity1);
    
                query.ColumnSet = new ColumnSet(true);
    
                LinkEntity linkEntity1 = new LinkEntity(entity1, relationshipEntityName, "accountid", "entityid", JoinOperator.LeftOuter);
    
                LinkEntity linkEntity2 = new LinkEntity(relationshipEntityName, entity2, "listid", "listid", JoinOperator.LeftOuter);
    
                linkEntity1.LinkEntities.Add(linkEntity2);
    
               
    
                string entity3 = "list";
    
                string entity4 = "campaign";
    
                string relationshipEntityName1 = "campaignitem";  
    
             
    
                LinkEntity linkEntity3 = new LinkEntity(entity3, relationshipEntityName1, "listid", "entityid", JoinOperator.LeftOuter);
    
                LinkEntity linkEntity4 = new LinkEntity(relationshipEntityName1, entity4, "campaignid", "campaignid", JoinOperator.LeftOuter);
    
                linkEntity3.LinkEntities.Add(linkEntity4);
                linkEntity2.LinkEntities.Add(linkEntity3);
                linkEntity1.LinkEntities.Add(linkEntity2);
    
    
                query.LinkEntities.Add(linkEntity1);

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get account and related information

    Hi,

    You can refer following links:

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/432669/get-account-and-related-information 

    Retrieve N:N relationship data in CRM using C# code – Murali Sabbavarapu CRM Blog (wordpress.com)

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,556 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,625 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans