web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'Microsoft.Xrm.Sdk.EntityReference'.

(0) ShareShare
ReportReport
Posted on by 75

QueryExpression query = new QueryExpression();
query.EntityName = "product";
query.ColumnSet.AllColumns = true;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
query.Criteria.Conditions.Add
(
new ConditionExpression("new_idproduct", ConditionOperator.Equal, document.Element("prestashop").Element("order").Element("associations").Element("order_rows").Elements().ElementAt(f).Element("product_id").Value)
);
EntityCollection entities = service.RetrieveMultiple(query);
if (entities.Entities.Count > 0) {
if (entitiespr[0].GetAttributeValue<EntityReference>("productid").Id.Equals(entities[0].Id))
{
f++;
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Ben Bartle Profile Picture
    5 on at

    Hello,

    When querying data within Dynamics, the primary field is always type Guid, rather than EntityReference. EntityReference is used for foreign keys, where you need to specify the entity logical name as well.

    If you change:

    if (entitiespr[0].GetAttributeValue<EntityReference>("productid").Id.Equals(entities[0].Id))


    to:

    if (entitiespr[0].GetAttributeValue<Guid>("productid").Equals(entities[0].Id))


    It should work.

    Thanks,

    Ben

  • Jf Marwen Profile Picture
    75 on at

    it did'nt work

  • Kokulan Profile Picture
    18,054 on at

    Hi

    Could you please make sure this is GUID not entity reference?

    document.Element("prestashop").Element("order").Element("associations").Element("order_rows").Elements().ElementAt(f).Element("product_id").Value - IS this GUID or Entity. reference? This should be GUID

     

    And change your 

    if (entitiespr[0].GetAttributeValue<EntityReference>("productid").Id.Equals(entities[0].Id))

    to 

    if (entitiespr[0].GetAttributeValue<Guid>("productid").Id.Equals(entities[0].Id))

     

  • Jf Marwen Profile Picture
    75 on at

    not works

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    The only place you are type casting is "GetAttributeValue<EntityReference>" so the error has to be within this statement. I noticed you have used entitiespr[0] not entities[0], is this a typo? It is possible that the same exception is occurring from some other code. Share your full code if this still doesn't work.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hello, you may try below :

    new ConditionExpression("new_idproduct", ConditionOperator.Equal, Guid(document.Element("prestashop").Element("order").Element("associations").Element("order_rows").Elements().ElementAt(f).Element("product_id").Value))

    Try it and let me know if it works

    Regards,

    Jitendra

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans