web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Read Global Optionset values from Web Resource (jscript)

(0) ShareShare
ReportReport
Posted on by 5

Hello,

I have a requirement to convert a two letter state code into an optionset value in a jscript web resource.  This is not being done on a form with the optionset on it.

Before I head down the rabbit hole, is there already a built-in function for doing this?  Get value from text, get text from value from a Global Optionset?

If not, it would seem that I need to do this via a XMLHttpRequest?  I could not see any XRM functions that would do this?

If XMLHttpRequest is the correct path, I have not been able to make it work. 

I have had success with this call in Postman: https://sandbox.crm.dynamics.com/api/data/v9.1/GlobalOptionSetDefinitions(Name='tpr_statesprovidences')  This does return all of the options in json format.

I have tried several variations of this code:

=====
var apiURL = clientURL + "/api/data/v9.1/GlobalOptionSetDefinitions(Name='tpr_statesprovidences')";

var tableOptions;

var req = new XMLHttpRequest();

req.open ("GET", apiURL, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

req.onreadystatechange == function ()
{
if (this.readyState == 4 /* complete*/ )
{
console.log("Made It Here Too", "");
req.onreadystatechange = null;
if (this.status == 200)
{
//tableOptions = JSON.parse(this.responseText);
var result = JSON.parse(this.response);
tableOptions = result;
}
else
{
var error = JSON.parse(this.response).error;
}
}
};
=====

I never get to the console.log statement.

Can anyone help with the simplest way to achieve this requirement?

Thanks.

Bryan Hunt

I have the same question (0)
  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi ccnwo,

    Your function will return information of a specific optionset record,

    I thought there was some error in your XMLHttpRequest,

    please try code below:

    var Name = 'decisionmaker';
    var record = retrieveOptionsetData(Name);
    console.log(record);
    console.log(record["FalseOption"]["Label"]["UserLocalizedLabel"].Label);
    
    function retrieveOptionsetData(optionsetName) {
      var data = null;
      var req = new XMLHttpRequest();
      req.open('GET', Xrm.Page.context.getClientUrl()   "/api/data/v9.0/GlobalOptionSetDefinitions(Name='"   optionsetName   "')", false);
      req.setRequestHeader("Accept", "application/json");
      req.setRequestHeader("OData-MaxVersion", "4.0");
      req.setRequestHeader("OData-Version", "4.0");
      req.setRequestHeader("Prefer", "odata.include-annotations=*");
      req.send();
      if (req.readyState == 4 /* complete */ ) {
        console.log('Hi. \n');
        if (req.status == 200) {
          data = JSON.parse(req.response);
        } else {
          var error = JSON.parse(req.response).error;
          console.log(error.message);
        }
      }
      return data;
    }

    Result:

    pastedimage1575270541269v1.png

    Regards,

    Clofly

  • Suggested answer
    ccnwo Profile Picture
    5 on at

    Clofly,

    Sorry for the delay in responding.

    I modified my routine to match your syntax and it worked perfectly.  

    Thanks for the assistance!

    Bryan

  • cloflyMao Profile Picture
    25,210 on at

    Hi Bryan,

    It's glad that your problem has been solved.

    Please kindly mark as verified if you had found my answer helped, it would be greatly appreciated. :) 

    Regards,

    Clofly

  • ccnwo Profile Picture
    5 on at

    Clofly,

    For some reason it will not let me mark your reply as the answer (permissions error).

    If you can ask the moderator to mark it correctly, please do.

    Thanks.

    Bryan

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans