Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Incorrect type of attribute value Microsoft.Xrm.Sdk.EntityReference

(0) ShareShare
ReportReport
Posted on by 474

Hello,

I could not find a D365 Customer Engagement forum, please let me know if there is a better place for me to post this.  Thank you.

We are new to Dynamics CRM, have been handed a large amount of existing code and a project that seems to be partially finished.


First, we were getting errors that 'invoice' entity doesn't contain attribute with Name = 'abcname'. 

So in the CRM solution we added 'abcname' as a Single Line of Text field.


Now we are getting the EntityReference exception in the subject. line of this post.  If we comment out the following line prior to our Create:


crmItem[crmRelationship.EntityReferenceColumn] = new EntityReference(relatedItem.LogicalName, relatedItem.Id);


Then we do not see the exception, and the Invoice creates but does not have data in 'abcname' or the customerid column.  They are both blank.


We have tried adding a relationship, changing the column type, and backing those out, but are still not getting data into the two fields.

(We now have a few CRM sites, since we didn't realize at first you cannot successfully change a data type.)


My question is: Where can I look in the Docs for the right way to use the EntityReference?  Is there sample code I should look at? 


Any suggestions for troubleshooting this?  We are fairly certain it is our new field causing the current problem but the error seems a bit generic and I'm not sure what it is trying to say. In one and two syllable words, please; due to our newbie-ness.

Thanks!

Emily H

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Incorrect type of attribute value Microsoft.Xrm.Sdk.EntityReference

    Hi,

    Here is sample code you can write similar-

                Entity myCase = new Entity("incident");
                myCase.Attributes["title"] = "Branch Installation for ";
                myCase.Attributes["subjectid"] = new EntityReference("entityname", recordid); //record id should be GUID
                Guid childCaseId = service.Create(myCase);
  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,052 on at
    RE: Incorrect type of attribute value Microsoft.Xrm.Sdk.EntityReference

    Hi Emily

    In Dynamics 365 CE, data is stored in Entities (Tables) and these entities are made up of Attributes (Columns).  An attribute (column) of type lookup (a reference to another entity),  is called Entity Reference in code (SDK - C#/JS) - Entity Reference identifies a record.

    The official definition for Lookup is : A field that allows setting a reference to a single record of a specific type of entity. 

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/customize/types-of-fields

    Lets take Account and Contact entity as an example.  An Account (typically represents a Company) can have many Contacts. This is a One (Account) to Many (Contacts) relationship scenario. To link  A Contact to an Account, we will have an Account lookup field on Contact (one easy way to remember this is a Contact is looking up to an Account). Like I mentioned about, a lookup filed in code is called EntityReference. 

     

    To identify a record using EntityReferece, we need to know which entity (logical name) and whats the record id (Id)

     

    Properties

    NameDescription
    System_CAPS_pubproperty ExtensionData

    Gets or sets the structure that contains extra data.

    System_CAPS_pubproperty Id

    Gets or sets the ID of the record.

    System_CAPS_pubproperty KeyAttributes

    Gets or sets the key attributes.

    System_CAPS_pubproperty LogicalName

    Gets or sets the logical name of the entity.

    System_CAPS_pubproperty Name

    Gets or sets the value of the primary attribute of the entity.

    System_CAPS_pubproperty RowVersion

    Gets or sets the row version.

    https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327579(v%3Dcrm.8)

    In the example of Accounts and Contacts, the contact entity has a lookup field called accountid. In c# code, you could set the value as shown below

    Entity contact = new Entity("contact");
    contact.Attributes["accountid"] = new EntityReference("account <-- This is logical name", new Guid("9f41efca-5e53-4ada-9a14-de7026944e0e - This is record ID"));
    ;

    Please follow the links below for more examples
    https://stackoverflow.com/questions/45537307/what-is-entity-reference-definition

    N
    ote : You mentioned that you guys inherited work from others, if the code is complaiing about a field, the field might exist in one envionment (eg.Dev) and might not have been deployed to the other environemnt (eg UAT or PROD).
    It might be worth checking if you guys have access to solutions that you could deploy and get all the missing feilds
    
                          

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans