Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Javascript to get Optionset Text from Value

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

  • Suggested answer
    C4p0 Profile Picture
    C4p0 25 on at
    RE: Javascript to get Optionset Text from Value

    Hi,friend!

    You can try the following method:

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/attributes/getselectedoption

    For Example:

    Xrm.Page.getAttribute("statuscode").getSelectedOption().text
    Xrm.Page.getAttribute("statuscode").getSelectedOption().value

    https://vjcity.blogspot.com/2019/05/generic-way-to-get-optionset-value-or.html?tdsourcetag=s_pcqq_aiomsg

  • Verified answer
    Vipin J Profile Picture
    Vipin J 1,583 on at
    RE: Javascript to get Optionset Text from Value

    Here is a generic way to get optionset values in C#, Rest API Javascript and CRM Formscript

    vjcity.blogspot.com/.../generic-way-to-get-optionset-value-or.html

  • Verified answer
    hajs Profile Picture
    hajs 2,601 on at
    RE: Javascript to get Optionset Text from Value

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

     

     

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Javascript to get Optionset Text from Value

    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
    Mohan Kumar  Profile Picture
    Mohan Kumar 90 on at
    RE: Javascript to get Optionset Text from Value

    Use below code to get OptionSet Value

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript to get Optionset Text from Value

    this for C# not java script

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript to get Optionset Text from Value

    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
    Community Member Microsoft Employee on at
    RE: Javascript to get Optionset Text from Value

    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;

                           }}

  • hajs Profile Picture
    hajs 2,601 on at
    RE: Javascript to get Optionset Text from Value

    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

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: Javascript to get Optionset Text from Value

    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)

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 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