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 :
Microsoft Dynamics CRM (Archived)

Javascript using Odata and should show value of the OptionSet Industrycode

(0) ShareShare
ReportReport
Posted on by 552

Hi there,

I'm just learning to get into the use of Odata. So far, so good...

But I tried to get the value of the industrycode (Optionset) of an account, but I don't get it. The Popup (alert) just shows me "undefined".
Could you please help me solve this problem? This would be perfect! It's just a really simple code for just playing around and learn.

var serverUrl = Xrm.Page.context.getClientUrl();
var queryUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=Name,CreatedOn";
var req = new XMLHttpRequest();
req.open("GET", queryUrl, 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=OData.Community.Display.V1.FormattedValue");
req.send();
var results = JSON.parse(req.responseText).d.results;
for (i=0;i<results.length;i++){
if(results[i]["Name"] == "Test Account"){
    var nametest = results[i]["Name"];
    var createdwhen = results[i]["CreatedOn"];
    var industrycode = results[i]["IndustryCode"];
    var industrycode_formatted = results[i]["industrycode@OData.Community.Display.V1.FormattedValue"];
    alert(nametest); 
    alert(createdwhen);
    alert(industrycode_formatted);
    }
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I see several issues:

    1. You use old OData v2.0 instead on new one - it will not bring labels the same way.

    2. You query only Name and CreatedOn field from CRM and not query IndustryCode field.

    To avoid many issues I can recommend to use CRMRestBuilder - github.com/.../2.5.0.0

  • Mick_4711 Profile Picture
    552 on at

    Thanks Andrew,

    thanks for your answer.

    Yes, I saw I forgot to query IndustryCode. I changed it.

    the queryurl is changed to:

    var queryUrl = serverUrl + "/api/data/v8.2/accounts?$select=createdon,name,industrycode";


  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    If it shows undefined that means that for account returned industrycode field is empty (null).

  • Mick_4711 Profile Picture
    552 on at

    Hi Andrew,

    Thx!!!

    I wanna do this tomorrow in a new way.

  • Mick_4711 Profile Picture
    552 on at

    Hi Andrew,

    now I got, what I wanted... I made it in two ways just as a little practice for me including readystate, etc.

    I didn't want to use REST Builder that much to learn, but it helped me in the end.

    =======================================================

    First way:

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=industrycode,name", true);

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

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

    if(results.value[i]["name"] == "Test Account"){

                   var industrycode = results.value[i]["industrycode"];

                   var industrycode_formatted = results.value[i]["industrycode@OData.Community.Display.V1.FormattedValue"];

                   var name = results.value[i]["name"];

    alert(name);

    alert(industrycode_formatted);}

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    =======================================================

    =======================================================

    Second way:

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=industrycode,name&$filter=name eq 'Test Account'", true);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) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var industrycode = results.value[i]["industrycode"];

                   var industrycode_formatted = results.value[i]["industrycode@OData.Community.Display.V1.FormattedValue"];

                   var name = results.value[i]["name"];

    alert(industrycode_formatted);

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans