web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

RETRIEVE OPTION SET METADATA FROM JAVA SCRIPT IN DYNAMICS CRM

chakkay Profile Picture chakkay 594

This blog provides the code and detailed explanation of retrieving option set meta data from JS by making WebAPI call to dynamics CRM server. The following code retrieves the metadata of statuscode field including both the label and value of all options present. This includes all the options related to open, won & lost status.

function _retrieveOptionSetMetadata () {

var _optionMetadata = CallWebAPI("EntityDefinitions?$filter=LogicalName eq 'opportunity'&$expand=Attributes($select=LogicalName;$filter=LogicalName eq 'statuscode')", false);

var _optionEntityMetadataId = _optionMetadata.value["0"].MetadataId;

var _optionAttributeMetadataId = _optionMetadata.value["0"].Attributes["0"].MetadataId;

var _optionAttributeMetadata = CallWebAPI("EntityDefinitions(" + _optionEntityMetadataId + ")/Attributes(" + _optionAttributeMetadataId + ")/Microsoft.Dynamics.CRM.StatusAttributeMetadata/OptionSet?$select=Options", false); //Rteurns the object which contains the statuscode fields metadata

var _length = _optionAttributeMetadata.Options.length; //returns the size of array containing the metadata of option set attribute

var _label0 = _optionAttributeMetadata.Options[0].Label.UserLocalizedLabel.Label //Enter the index of array to get the option set label

var _value0 = _optionAttributeMetadata.Options[0].value // Enter the index of array to get the option set integer value

}
            
function CallWebAPI(webapiquery,async) {

var _url = Xrm.Page.context.getClientUrl() + "/api/data/v9.1/";

var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", _url + webapiquery.replace(/[{}]/g, ''), async);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

if (query.indexOf("fetchXml=") > -1) retrieveReq.setRequestHeader("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'");

retrieveReq.send();

        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                return JSON.parse(retrieveReq.responseText);
            }

            else {
                return null;
            }
        }
}


Executing above code will return a final object which contains the statuscode attributes metadata (label & value) in a array. Looping through each array element would result in achieving the final goal.

RETRIEVE ONLY "LOST" STATUS REASON OPTIONS

In order to retrieve only status reason options which are related to status "Lost", use the below command:

_optionAttributeMetaData.Options[0].State // this returns the statecode. 0 for 'open', 1 for 'won', 2 for 'lost'

Based on this you can filter the _optionAttributeMetaData to include only "LOST" status options.

Comments

*This post is locked for comments