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 :
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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans