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 get a text value from option set

(0) ShareShare
ReportReport
Posted on by 645

I'm using crm 2016 and I want to know if there is a way to get a text value from an Optionset or must I create an ENUM and according to my INT value retrieve my text value  from my OptionSet.

I searched in this forum and found this option : 

OptionSetValue opProductType = new OptionSetValue();
opProductType = (OptionSetValue)pCall.xnes_test2;
var optionValue = opProductType.Value;

I get the value 2 but in my field the value 2 = test2.

Is there a way ? or should I fall to a world of enums

thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    You can try this,

    var optionValue = opProductType.Text;

    Thank You

  • sahara Profile Picture
    645 on at

    Hi Ridhima Text is not an option :-/

  • Suggested answer
    Community Member Profile Picture
    on at

    Okay. Try to use toString() to get convert it into string.

    var optionValue = (opProductType.Value).to string();

  • sahara Profile Picture
    645 on at

    Hi Ridhima I will get that value -> 2 just as string...

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Hope the following link may help you

    [View:https://community.dynamics.com/crm/f/117/t/250277]

    [View:https://stackoverflow.com/questions/23359880/retrieve-the-value-selected-in-optionset-field-and-display-ita-value-in-a-text-f]

    Thanks

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello Sahara,

    Normally in direct xrm methos you can not get the text value , you need to retrieve from metadata.

    Here is the c# reference

          /// 
            ///This method will gives you the optionset text based on value 
            /// 
            ///  Entity Name of otionset field 
            /// Optionset field Name
            ///  Option Set Value
            /// Organization Service
            /// 
    
            public string GetOptionSetTextUsingValue(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
            {
    
                var attReq = new RetrieveAttributeRequest();
                attReq.EntityLogicalName = entityName;
                attReq.LogicalName = fieldName;
                attReq.RetrieveAsIfPublished = true;
                var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
                var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;
                return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
    
            }

    [View:http://microsoftcrmtechie.blogspot.in/2016/03/retrieve-optionset-text-in-crm-2011.html?m=1:750:50

    [View:https://community.dynamics.com/enterprise/b/crmmemories/archive/2017/04/20/retrieve-option-set-metadata-in-c:750:50

    Here is the javascript reference

    [View:https://nishantrana.me/2014/02/05/retrieve-optionset-label-using-sdk-metadata-rertrieveattribute-method-in-javascript-crm-2011/:750:50

    [View:https://meghshyam.wordpress.com/2014/01/29/retrieving-the-optionset-label-using-javascript-in-crm-2011crm-2013/:750:50

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hello Saraha,

    Use this link.

    community.dynamics.com/.../246368

    Hope it will help you!

  • Vijayaragavan           Profile Picture
    10 on at

    Hi Sahara ,

    Try this string OptionsetText = targetEntity.FormattedValues[OptionSet_AttributeName];

    or you need to retrieve from metadata.

    RetrieveAttributeRequest Req = new RetrieveAttributeRequest

               {

                   EntityLogicalName = entity.LogicalName,

                   LogicalName = attributeName,

                   RetrieveAsIfPublished = true

               };

               RetrieveAttributeResponse Resp = (RetrieveAttributeResponse)service.Execute(Req);

               EnumAttributeMetadata data = (EnumAttributeMetadata)Resp.AttributeMetadata;

               foreach (OptionMetadata picklist in data.OptionSet.Options)

               {

                   // Check int value.                

                   optionsetText = picklist.Label.UserLocalizedLabel.Label;

               }

    Thanks and Regards ,

    Vijayaragavan

  • Suggested answer
    Anwesh Reddy Profile Picture
    10 on at

    You can find it under Entity formatted attribute values.

  • Kunal G Profile Picture
    37 on at
    function getOption(executionContext) {
    var formContext = executionContext.getFormContext();
        var optionSetAttribute = formContext.getAttribute(myOptionsetField);
        if (optionSetAttribute  != null) {
            var text = optionSetAttribute.getText();
    var value = optionSetAttribute.getValue();
        }
    }

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