JensB
JensB

Reputation: 6880

Get optionset text from OData query in CRM web api

https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset

The above query selects all values in an optionset field in CRM. The return data looks something like this:

{
    {
        "@odata.etag":"W/\"112607639\"","custom_optionset":285960000,"accountid":"a08f0bd1-e2c4-e111-8c9a-00155d0aa573"
    },
    {
        "@odata.etag":"W/\"112615384\"","custom_optionset":285960010,"accountid":"a18f0bd1-e2c4-e111-8c9a-00155d0aa573"
    }
}

I don't want the value of the optionset. I want the associated text label. How do I get this?

Upvotes: 8

Views: 9267

Answers (3)

Henrik H
Henrik H

Reputation: 5812

I find it easiest to use XrmQuery for calling the Web API in CRM (full disclosure: I work for the company that developed this). No manual fiddling with headers and XMLHttpRequests is needed, and you get full strongly typed support for the entities and fields in your own CRM.

As an example, retrieving a specific account and selecting the industrycode field looks as follows:

    XrmQuery.retrieve(a => a.accounts, "5B86C6EB-DE6D-E611-80DF-C4346BADF080")
        .select(a => [a.industrycode])
        .includeFormattedValues()
        .execute(a => {
            console.log("Industry is: " + a.industrycode_formatted);
        });

Adding .includeFormattedValues() automatically gives you access to a field simply called industrycode_formatted.

For an account with the following optionset value: Industry in CRM

The code above will return:

Industry is: Broadcasting Printing and Publishing

Upvotes: 3

Aron
Aron

Reputation: 3935

If you use Jason Lattimer's CRM RESTBuilder Solution, it creates a query like this, which includes the header req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(674D7FDC-47AE-E711-8108-5065F38A3BA1)?$select=accountid,industrycode", 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 result = JSON.parse(this.response);
            var accountid = result["accountid"];
            var industrycode = result["industrycode"];
            var industrycode_formatted = result["[email protected]"];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

And the result includes the option set label:

{
@odata.context:"https://myorg.crm.dynamics.com/api/data/v8.2/$metadata#accounts(accountid,industrycode)/$entity",
@odata.etag:"W/"1959756"",
accountid:"674d7fdc-47ae-e711-8108-5065f38a3ba1",
[email protected]:"Accounting",
industrycode:1
}

Upvotes: 4

To get optionset text using webapi, use below snippet in request header.

req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");

This will return the picklist text similar to lookup FormattedValue.

enter image description here

Entire code example:

function retrieveEntity(entityName, Id, columnSet) {
    var serverURL = Xrm.Page.context.getClientUrl();
    var Query = entityName + "(" + Id + ")" + columnSet;
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.2/" + Query, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data != null {
                        alert(data["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"]); //for lookup text
                        alert(data["[email protected]"]); //for optionset text
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        req.send();
    }

Reference.

Upvotes: 18

Related Questions