Wednesday, May 22, 2019

Generic way to get OptionSet value or Text in Dynamic 365 CRM


Here are C# methods

public static string GetOptionSetTextFromValue(string entityName, string attributeName, int value)
{
    string optionsetText = string.Empty;
    var attributeRequest = new RetrieveAttributeRequest
    {
        EntityLogicalName = entityName,
        LogicalName = attributeName,
        RetrieveAsIfPublished = true
    };
    // Execute the request.
    var attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest);
    // Access the retrieved attribute.
    var attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata;
    // Get the current options list for the retrieved attribute.
    var optionList = (from o in attributeMetadata.OptionSet.Options
                        select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();

    optionsetText = optionList.Where(o => o.Value == value)
                                .Select(o => o.Text)
                                .FirstOrDefault();
    return optionsetText;
}


public static int GetOptionSetValueFromLabel(string entityName, string attributeName, string optionsetText)

{
    int optionSetValue = 0;
    var attributeRequest = new RetrieveAttributeRequest()
    {
        EntityLogicalName = entityName,
        LogicalName = attributeName,
        RetrieveAsIfPublished = true
    };
    // Execute the request.
    var attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest);
    // Access the retrieved attribute.
    var picklistAttributeMetadata = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;
    // Get the current options list for the retrieved attribute.
    OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;
    var optionList = (from o in optionsetMetadata.Options
                        select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();

    optionSetValue = (int)optionList.Where(o => o.Text.ToLower() == optionsetText.ToLower())
                                .Select(o => o.Value)
                                .FirstOrDefault();
    return optionSetValue;

}


Here is REST API Way

We need to include Formatted Values to get OptionSet Labels

var req = new XMLHttpRequest();
var fetchStatement = "/api/data/v9.1/contacts(EF5D6F09-399A-E811-A966-000D3AB0C08C)?$select=cm_language";
req.open("GET", Xrm.Page.context.getClientUrl() + fetchStatement, 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=\"*\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            //OptionSet Value
            var cm_language = result["cm_language"];
            //OptionSet Label (Text)
            var cm_language_formatted = result["cm_language@OData.Community.Display.V1.FormattedValue"];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};

req.send();


Here is a Form Script



Xrm.Page.getAttribute("cm_customerservicelanguage").getSelectedOption().value 
Xrm.Page.getAttribute("cm_customerservicelanguage").getSelectedOption().text



1 comment:

RoyceRobbie said...

Wow, Great information and it is very useful for us.

Crm software development company in chennai