Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 352
JensB Avatar asked Dec 01 '22 11:12

JensB


2 Answers

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.

like image 91
Arun Vinoth - MVP Avatar answered Dec 05 '22 16:12

Arun Vinoth - MVP


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
}
like image 29
Aron Avatar answered Dec 05 '22 18:12

Aron