Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to set optionset field value by paaing it's label instead of value using webapi and c#

Posted on by 80

Hi there,

We are creating a service that updates few fields in custom entity. One of those fields type is optionset. In the source data, we only have labels of these optionset. But i couldn't find a way to set the Label as optionset field value. For example below are the options of my optionset field.

Label                     Value

------------------------------------

Normal                  640330000

Medium                 640330001

High                      640330002

In my source data i only have Label values like Normal, Medium and High. Is there a way that i can pass these Normal as a value of RestAPI and Set it. 

Please suggest.

Thanks,

Venu

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: How to set optionset field value by paaing it's label instead of value using webapi and c#

    Hi,

    You can use below web api code to get the optionset value based on label and then pass value to rest api.

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/stringmaps?$select=attributename,attributevalue,objecttypecode,value&$filter=attributename eq 'purchasetimeframe' and  objecttypecode eq 'lead'", 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 attributename = results.value[i]["attributename"];
                    var attributevalue = results.value[i]["attributevalue"];
                    var attributevalue_formatted = results.value[i]["attributevalue@OData.Community.Display.V1.FormattedValue"];
                    var objecttypecode = results.value[i]["objecttypecode"];
                    var value = results.value[i]["value"];
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

    You can not directly pass label to rest api. It will not work. You have to first get the optionset value and use that to pass to rest api.

    I have used lead entity and purchasetimeframe in filter condition. you can replace that with your field and entity name.

    Response from Web API

    {

    • @odata.etag:"W/"2238179"",
    • attributename:"purchasetimeframe",
    • attributevalue@OData.Community.Display.V1.FormattedValue:"0",
    • attributevalue:0,
    • objecttypecode@OData.Community.Display.V1.FormattedValue:"Lead",
    • objecttypecode:"lead",
    • value:"Immediate",
    • stringmapid:"31ef31b0-a5b3-ea11-a813-000d3a1bbc7a"

    }

    If found helpful, Please mark my answer verified.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: How to set optionset field value by paaing it's label instead of value using webapi and c#

    Hi, 

    Label/value are stored in stringMap entity. You can query this entity to get the value by using the label: 

    https://exptechsolutions.blogspot.com/2017/09/know-how-d365-crm-get-optionset-label.html

    You can also use early-Bound classes, please check this thread: 

    https://stackoverflow.com/questions/51431233/c-sharp-set-crm-optionsetvalue-using-only-the-string-name-of-the-option/51431606

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