Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript using Odata and should show value of the OptionSet Industrycode

Posted on by 550

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

  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Javascript using Odata and should show value of the OptionSet Industrycode

    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();

  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Javascript using Odata and should show value of the OptionSet Industrycode

    Hi Andrew,

    Thx!!!

    I wanna do this tomorrow in a new way.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript using Odata and should show value of the OptionSet Industrycode

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

  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Javascript using Odata and should show value of the OptionSet Industrycode

    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
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript using Odata and should show value of the OptionSet Industrycode

    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

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans