Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

(0) ShareShare
ReportReport
Posted on by 509

Hi

During a code upgrade (to 8.2)  I'm getting the following error.

Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

my code is

---

if (DBStatus.custom_field != null)
InsertStatusHistory("Investigation Outcome Status", ((EntityReference)DBStatus.custom_field).Name, _caseID, service);

---

(EntityReference)DBStatus.custom_field).Name is the cause of the error

Can anybody let me know what the upgrade to this code should read like?

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,321 on at
    RE: Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

    Hi,

    Thank you for your query.

    If you are sure that the DBStatus.custom_field  is an option set, you should pass OptionSetValue or string label instead of Entity Reference (depending on your logic).

    You can get the OptionSet Label using Formatted values collection form your entity object.

    if (DBStatus.custom_field != null)
    {
        string dbStatusLabel = DBStatus.FormattedValues["custom_fiel_name"];
    
        InsertStatusHistory("Investigation Outcome Status", dbStatusLabel, _caseID, service);
    
    }

    Please note, above code is for your reference, you might need to modify it to match your logic.

    More details on getting formatted values:

    Get OptionSet Label using FormattedValues property of Entity in Plugin in CRM – Nishant Rana's Weblog

  • Torrado Profile Picture
    Torrado 1,270 on at
    RE: Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

    Hello,

    Then, please refer to this article, about how to set a value for OptionSet field: crmbusiness.wordpress.com/.../

    Hope this help you.

  • Lee-Martin Profile Picture
    Lee-Martin 509 on at
    RE: Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

    Thank you, but it is an optionset not a lookup

  • Suggested answer
    Torrado Profile Picture
    Torrado 1,270 on at
    RE: Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'Microsoft.Xrm.Sdk.EntityReference'

    Hello Lee-Martin,

    It seems that you are trying to set a value for a lookup attribute but not using the correct object properties. Probably you are using a constructor that cannot be used with earlier versions of Dynamics 365.

    You should use something like:

    entity.Attributes["lookupFieldName"] = new EntityReference(string logicalName, Guid id); 

    Hope this helps you.

    Please mark the answer as verified if helpful. That will help others in future.

     

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans