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)

Javascript to get Optionset Text from Value

(0) ShareShare
ReportReport
Posted on by 2,601

I've found code which nearly does what I want to do (from http://guruprasadcrm.blogspot.co.uk/2011/07/retrieving-optionset-lable-data-using.html)

I can't get the value of the OptionSet text in the code though.

This is the modified code I have:

function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName, OptionSetValue)
{

var optionSetText = "";

SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,
function (result) {
for (var i = 0; i < result.OptionSet.Options.length; i++) {
var loopText = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
var loopValue = result.OptionSet.Options[i].Value;
-- I want to do a check here but this is in a different function
}
},
function (error) { }
);

return optionSetText;

}

 

Any ideas?

*This post is locked for comments

I have the same question (0)
  • Rajkumar Rajaraman Profile Picture
    on at
  • CU21020959-0 Profile Picture
    2,601 on at

    The optionset isn't on the form so this would not work.  I need to make some kind of OData call.

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at

    You can refer this:

    meghshyam.wordpress.com/.../retrieving-the-optionset-label-using-javascript-in-crm-2011crm-2013

    Use

    SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,

    function (result)

    Instead of

    SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, “00000000-0000-0000-0000-000000000000”, true,

              function (result)

  • CU21020959-0 Profile Picture
    2,601 on at

    I have seen this page but the function doesn't actually do anything with the text and value of the optionset.  I want to return the text if it matches a value

  • Community Member Profile Picture
    on at

    use this

    int   r = ((OptionSetValue)e["f"]).Value;       // f is name of the option set the filed

        to get text of this data  we write :

           RetrieveAttributeRequest a = new RetrieveAttributeRequest()

                       {

                           EntityLogicalName = e.LogicalName,

                           LogicalName = "f",

                           RetrieveAsIfPublished = true

                       };

                       RetrieveAttributeResponse y = (RetrieveAttributeResponse)service.Execute(a);

                       PicklistAttributeMetadata l = (PicklistAttributeMetadata)y.AttributeMetadata;

                       OptionMetadata[] o = l.OptionSet.Options.ToArray();

                       foreach (OptionMetadata m in o) {

                           if(m.Value==r){                                        // r is data value

                              string r2 = m.Label.UserLocalizedLabel.Label;

                           }}

  • Community Member Profile Picture
    on at

    to get option set value use

    int   r = ((OptionSetValue)e["f"]).Value;       // f is name of the option set the filed

    and to get the text for this value use the other code

  • Community Member Profile Picture
    on at

    this for C# not java script

  • Suggested answer
    Mohan Kumar  Profile Picture
    90 on at

    Use below code to get OptionSet Value

    Xrm.Page.getAttribute("fieldname").getValue()

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You can use webapi to retrieve the values and labels from particular optionsset, which will return the data in JSON format.

    The following webapi call will return the accountcategorycode values in json format for the account entity, You can do the manipulation after that:

    orgname.contoso.com/.../EntityDefinitions(LogicalName='account')/Attributes(LogicalName='accountcategorycode')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)

    This will return the following:

    { "@odata.context":"orgname.contoso.com/.../v8.2$metadata#EntityDefinitions('account')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata(LogicalName,OptionSet,GlobalOptionSet,OptionSet(Options),GlobalOptionSet(Options))/$entity","LogicalName":"accountcategorycode","MetadataId":"118771ca-6fb9-4f60-8fd4-99b6124b63ad","OptionSet@odata.context":"orgname.contoso.com/.../v8.2$metadata#EntityDefinitions('account'#/Attributes#118771ca-6fb9-4f60-8fd4-99b6124b63ad)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet(Options)/$entity","OptionSet":{ "Options":[ { "Value":1,"Label":{ "LocalizedLabels":[ { "Label":"Preferred Customer","LanguageCode":1033,"IsManaged":true,"MetadataId":"0bd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } ],"UserLocalizedLabel":{ "Label":"Preferred Customer","LanguageCode":1033,"IsManaged":true,"MetadataId":"0bd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } },"Description":{ "LocalizedLabels":[ ],"UserLocalizedLabel":null },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null },{ "Value":2,"Label":{ "LocalizedLabels":[ { "Label":"Standard","LanguageCode":1033,"IsManaged":true,"MetadataId":"0dd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } ],"UserLocalizedLabel":{ "Label":"Standard","LanguageCode":1033,"IsManaged":true,"MetadataId":"0dd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } },"Description":{ "LocalizedLabels":[ ],"UserLocalizedLabel":null },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null } ],"MetadataId":"b994cdd8-5ce9-4ab9-bdd3-8888ebdb0407" },"GlobalOptionSet":null }

    Hope this helps.

  • Suggested answer
    CU21020959-0 Profile Picture
    2,601 on at

    So I finally worked out how to get this working in CRM 2015.

    The working function is similar to the one I posted in my question:

    function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName, OptionSetValue)
    {
    var optionSetText = "";

    SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,
    function (result) {
    for (var i = 0; i < result.OptionSet.Options.length; i++) {
    var loopText = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
    var loopValue = result.OptionSet.Options[i].Value;
    if (OptionSetValue == loopValue) {
    optionSetText = loopText;
    }
    }
    },
    function (error) { }
    );

    return optionSetText;
    }

     

    I also had to edit a line in SDK.MetaData.js to make the SDK.MetaData.RetrieveAttribute synchronous.  I wasn't entirely happy about this but it did work. (found in a comment by Veer Balla in https://community.dynamics.com/crm/f/117/t/167882)

    change
         req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", true);
    to
         req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", false);

     

     

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