Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

how to get a text value from option set

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

  • Kunal G Profile Picture
    Kunal G 2 on at
    how to get a text value from option set
    function getOption(executionContext) {
    var formContext = executionContext.getFormContext();
        var optionSetAttribute = formContext.getAttribute(myOptionsetField);
        if (optionSetAttribute  != null) {
            var text = optionSetAttribute.getText();
    var value = optionSetAttribute.getValue();
        }
    }
  • Suggested answer
    Anwesh Reddy Profile Picture
    Anwesh Reddy 10 on at
    RE: how to get a text value from option set

    You can find it under Entity formatted attribute values.

  • RE: how to get a text value from option set

    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
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get a text value from option set

    Hello Saraha,

    Use this link.

    community.dynamics.com/.../246368

    Hope it will help you!

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: how to get a text value from option set

    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
    Community Member Microsoft Employee on at
    RE: how to get a text value from option set

    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

  • sahara Profile Picture
    sahara 645 on at
    RE: how to get a text value from option set

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

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get a text value from option set

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

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

  • sahara Profile Picture
    sahara 645 on at
    RE: how to get a text value from option set

    Hi Ridhima Text is not an option :-/

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get a text value from option set

    Hi,

    You can try this,

    var optionValue = opProductType.Text;

    Thank You

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans