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)

What is the use of Entity Reference Class

(0) ShareShare
ReportReport
Posted on by 437

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

I have the same question (0)
  • Verified answer
    Dynamics Objects 365 Profile Picture
    550 on at
    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.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator 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
    Dynamics365 Rocker Profile Picture
    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.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/forums/thread/details/?threadid=87186e9e-2636-4d99-a022-229abc615ddb%27nvOpzp;%20AND%201=1%20OR%20(%3C%27%22%3EiKO))," - Too many )'s.