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 get option set value in entity collection

(0) ShareShare
ReportReport
Posted on by 435

Hi Friends,

I need to get the option set value while looping through the entity collection in a console application .

foreach (Entity r in ec.Entities)
{
    string optionsetChanceofScuccess = r.Attributes["angus_chanceofsuccess"].ToString();  // not getting value 

      // string optionsetChanceofScuccess = r.FormattedValues["angus_chanceofsuccess"].ToString();  // giving error 

}

Please suggest.

*This post is locked for comments

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get option set value in entity collection

    Thanks it worked.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: How to get option set value in entity collection

    You said that you wanted value and this is value. What you really mean is label. Getting label is more difficult and FormattedValue option works only in certain scenarios. This answer should help you:

    stackoverflow.com/.../7708157

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How to get option set value in entity collection

    Hi,

    entity.FormattedValues[attrobiteName] should work.

    Can you please let us know what error you are receiving when you are trying to the formatted Value.

    Is it possible that the optionSet does not have a value on the record that you are trying to retrieve, in which case you would need to do the following before:

    string optionSetValue = r.Contains(fieldname) ? r.FormattedValues[fieldname] : string.Empty;

    Let us know regarding the error.

  • Suggested answer
    SergiiChvyr Profile Picture
    SergiiChvyr on at
    RE: How to get option set value in entity collection

    If it already returns 100003, then check r.FormattedValues["angus_chanceofsuccess"].ToString();

    again

  • SergiiChvyr Profile Picture
    SergiiChvyr on at
    RE: How to get option set value in entity collection

    Hi, I think you are not getting this attribute in query which results in this collection.

    Otherwise, your code must work(of course, double check attribute name).

    Additionally, you may use Field Level security for the field and user does not have appropriate access to get this value.

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get option set value in entity collection

    Its not working my option set has 10%, 20%, 30%, 40%, 50%,

    so string optionsetChanceofScuccess  return  the same , Now it is returning 100003 only

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: How to get option set value in entity collection

    Try this:

    foreach (Entity r in ec.Entities)
    {
        string optionsetChanceofScuccess = r.GetAttributeValue<OptionSetValue>("angus_chanceofsuccess").Value.ToString(); 
    //some logic }

    Also make sure that your entity "has" this attribute (if you obtained this entity using some query for example), maybe you have not selected this attribute in your query.

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…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans