Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Read Global Optionset values from Web Resource (jscript)

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

  • ccnwo Profile Picture
    ccnwo 5 on at
    RE: Read Global Optionset values from Web Resource (jscript)

    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

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Read Global Optionset values from Web Resource (jscript)

    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

  • Suggested answer
    ccnwo Profile Picture
    ccnwo 5 on at
    RE: Read Global Optionset values from Web Resource (jscript)

    Clofly,

    Sorry for the delay in responding.

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

    Thanks for the assistance!

    Bryan

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Read Global Optionset values from Web Resource (jscript)

    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

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,253 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