Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

What is the use of Entity Reference Class

Posted on by 435

Hi All, 
A small doubt but very important one .

What is the use of Class EntityRefernce( LogicalName , Guid)

This class has  5 overloads . So What is the use of this.I have seen many places in programs but not clear about it.

Is it if want to fetch the record of an entity which might be linked with another entity then that time we use.

like  in Account we want to fetch contact records 

then we can EntityRefernce( 'Contact' , GuidofContact)

Pls correct me if I am wrong .Also pls explain it different use with examples

Thank you

*This post is locked for comments

  • Verified answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: What is the use of Entity Reference Class

    Yes, you are correct.

    I hope you are aware about lookup field in CRM. Whenever we want to assign a value in lookup then we have to use Entity reference.

    Lookup holds value that has " Name", "GUID" and "Entity Name". Entity reference holds all three values in single object.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: What is the use of Entity Reference Class

    Yes, you are correct. This is the data type for the lookup fields. This means you you fetch a record, lookup fields will be return Entity Reference object which contains the name of the lookup entity and the unique guid.

    Below statement retrieves the primarycontactid of the account

    =================

    using (service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null))

               {

                   var accountId = new Guid("00AAE0D5-D58B-E811-8153-480FCFF12AC1");

                   var accountEntityName = "account";

                   var accountRecord = service.Retrieve(accountEntityName, accountId, new ColumnSet("true"));

                   if (accountRecord.Contains("primarycontactid"))

                   {

                       var primaryContact = (EntityReference)accountRecord["primarycontactid"];

                       var contactRecord = service.Retrieve(primaryContact.LogicalName, primaryContact.Id, new ColumnSet("true"));

                       Console.WriteLine("Primary Contact Fullname: " + contactRecord["fullname"]);

                   }                

               }

    ===================

    Hope this helps.

  • Verified answer
    RE: What is the use of Entity Reference Class

    As the name suggests it is reference to another entity.

    It is used to set the Lookup fields.

    Ex : if in the UI you have the contact lookup on account form, then if you want to set the contact lookup via code, you have to use EntityReference.

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