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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to retrieve option set value?

(0) ShareShare
ReportReport
Posted on by 445

Hi,

I've an option set field in my custom entity.

I'm able to retrieve text filed values, but when I tried to retrieve option set value I'm receiving the following error.

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

Though I've declared the column logical name in the Columnset.

How to retrieve the option set value from my custom entity?

*This post is locked for comments

I have the same question (0)
  • Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Could you post your code with how toy retrieve the entity and access the attribute?

    Thanks,

     Aiden

  • Suggested answer
    Community Member Profile Picture
    on at

    Retrieving the value of the optionset should work if the name of the field is correct. If you are using late bound in the code, can yo verify that you are using the field name in small characters only? for ex. new_fieldname and not new_FieldName.

  • Elangamban Profile Picture
    445 on at

    Hi,

    My code is like as follows:

    QueryExpression query1 = new QueryExpression { EntityName = "MyEntity", ColumnSet = new ColumnSet("new_branch", "new_optionsetvalues") };
     

    EntityCollection results1 = Service.RetrieveMultiple(query1);

    string businessdivisionText = null;

    foreach(Entity e1 in results1.Entities)

    {

        businessdivisionText = (string)e1["new_optionsetvalues"];

    }

  • Elangamban Profile Picture
    445 on at

    Hi Vikas,

    Yes, I've verified all are in small characters.

    Thanks

  • Verified answer
    Community Member Profile Picture
    on at

    If you are trying to get the value the use

    businessdivisionText = e1.GetAttributeValue<OptionSetValue>("new_optionsetvalues").Value.ToString();

    If trying to get the text, use below:

    businessdivisionText = e1.FormattedValues["new_optionsetvalues"].ToString();

    Please make sure you have a value selected in the dropdown. Otherwise you will get the same error. To avoid that error you can apply the check before getting the value

    if(e1.Containts("new_optionsetvalues"))

    {

    businessdivisionText = e1.GetAttributeValue<OptionSetValue>("new_optionsetvalues").Value.ToString();

    }

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi,

    Just try to check field is present or not in the collection of properties:

    foreach (Entity e1 in results1.Entities)

    {

    if (e1.Attributes.ContainsKey("new_optionsetvalues"))

    {

       businessdivisionText = ((string)(e1.Attributes["new_optionsetvalues"])).Value;

    }

    }

    And check this one:

    stackoverflow.com/.../retrieve-the-value-selected-in-optionset-field-and-display-ita-value-in-a-text-f

  • Elangamban Profile Picture
    445 on at

    Thanks Vikas,

    That works!!!!!

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans