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)

formatted values are showing up as Undefined?!

(0) ShareShare
ReportReport
Posted on by

I am using the Web API to retrieve formatted values from a related entity. I essential want the text value of each field to be put into one string separated by a hyphen. Here is the code I am using to do this: 

var req_b = new XMLHttpRequest();
        req_b.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contracts(064A66F9-96A7-E811-816A-480FCFE97E21)?$expand=contract_line_items($select=new_locationgrouping,new_servinggroup,new_servingtime)", true);
        req_b.setRequestHeader("OData-MaxVersion", "4.0");
        req_b.setRequestHeader("OData-Version", "4.0");
        req_b.setRequestHeader("Accept", "application/json");
        req_b.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req_b.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req_b.onreadystatechange = function () {
            if (this.readyState === 4) {
                req_b.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.response);
                    var contractid = result["contractid"];
                    for (var a = 0; a < result.contract_line_items.length; a++) {
                        var locationdestination = result.contract_line_items[a]["new_locationdestination"];
                        var servinggroup = result.contract_line_items[a]["new_servinggroup"];
                        var servinggroup_formatted = result.contract_line_items[a]["new_servinggroup@OData.Community.Display.V1.FormattedValue"];
                        var servingtime = result.contract_line_items[a]["new_servingtime"];
                        var servingtime_formatted = result.contract_line_items[a]["new_servingtime@OData.Community.Display.V1.FormattedValue"];

                        var optionValue = 100000000 + a;
                        var optionText = servinggroup_formatted + "-" + servingtime_formatted + "-" + locationdestination; 

                        Xrm.Page.getControl("new_contractserving").addOption({ value: optionValue, text: optionText });
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req_b.send();


For some reason I am getting: "undefined-undefined-undefined" in the new_contractserving field. What is going on here? I just want the text value of servinggroup and servingtime (which are options sets) along with locationdestination (which is a text field). Why am I getting undefined?!

odata.JPG

*This post is locked for comments

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    new_locationdestination is undefined because it's not in the select list in the query.

    As for the optionset values, try using the specific Prefer option, rather than the wildcard:

        req_b.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");  //for formatted values


    If that doesn't work, the other thing to check is that the entity and option set have been published, and also that you have labels for the option set in the user's language

  • Community Member Profile Picture
    on at

    Thank you so much for that. I have another related question. When I click on any one of these options, it does not select it at all! The field just goes back to blank. Is there something else I have to do after:

    Xrm.Page.getControl("new_contractserving").addOption({ value: optionValue, text: optionText });

    Thank you in advance!

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 April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans