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

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

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans