Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to retrieve option set value?

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

  • Elangamban Profile Picture
    Elangamban 445 on at
    RE: How to retrieve option set value?

    Thanks Vikas,

    That works!!!!!

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    Jharana Baliyar Singh 2,665 on at
    RE: How to retrieve option set value?

    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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve option set value?

    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();

    }

  • Elangamban Profile Picture
    Elangamban 445 on at
    RE: How to retrieve option set value?

    Hi Vikas,

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

    Thanks

  • Elangamban Profile Picture
    Elangamban 445 on at
    RE: How to retrieve option set value?

    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"];

    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve option set value?

    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.

  • Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: How to retrieve option set value?

    Hi,

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

    Thanks,

     Aiden

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans