Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to access a specific value of a lookup field via service client - early bound classes

Posted on by 390

Hello People,

   I have been trying to access a specific follow of a lookup field, I am kind of new to Dynamic CRM, so my code might be a bit off. My coe is below. Thanks 

//var orgContext = new MedinetCRMService(GetOrgService());

                new_hospital hospital = new new_hospital();
                hospital.new_AddressOne = HAddressTextBox.Text.ToString();
                hospital.new_name = HNameTextBox.Text.ToString();

            Account trust = new Account();
            QueryExpression trustQuery = new QueryExpression();
            trustQuery.EntityName = "account";
            trustQuery.ColumnSet = new ColumnSet("name");
            trustQuery.Criteria.AddCondition(trust.Name, ConditionOperator.Equal, "BIRMINGHAM TRUST");

            Guid accountid = Guid.Empty;
            string accountName = "";
            EntityCollection Collection = Service().RetrieveMultiple(trustQuery);
            foreach (Account account in Collection.Entities)
            {
                accountid = account.Id;
               accountName = account.GetAttributeValue<string>("name");
            }


            
            //Account account = (Account)GetOrgService().Retrieve(trust.LogicalName, trustID, new Microsoft.Xrm.Sdk.Query.ColumnSet("name"));
            CrmEntityReference trustid = new CrmEntityReference(trust.LogicalName, accountid);
            hospital.new_OwningTrustId = trustid;
            Service().Create(hospital);
            MessageBox.Show("succesfully added a hospital record");

I know, I am not meant to use retrieveMultiple at the first place. Thanks in advance

*This post is locked for comments

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: How to access a specific value of a lookup field via service client - early bound classes

    Thanks Drew, Microsoft have a way in complicating simple code, but this helped.

  • Verified answer
    Drew Poggemann Profile Picture
    Drew Poggemann 9,079 on at
    RE: How to access a specific value of a lookup field via service client - early bound classes

    Hi Victor, the following provides a very detailed example for you to follow, hopefully this helps!

    msdn.microsoft.com/.../dn879513(v=mdm.8).aspx

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans