Get Status Reasons (statuscode) for a specific status (Statecode) from metaData

Welcome to my old Blogspot blog! You can read this post on our new website: Read this post on our new website 

Following is sample code for getting statuses for an entity from metadata

function getStatusAttributeMetadata(entityName) {  
    var data = null;
    var webApiQuery = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/EntityDefinitions(LogicalName='" + entityName + "')/Attributes/Microsoft.Dynamics.CRM.StatusAttributeMetadata?$expand=OptionSet";

    var req = new XMLHttpRequest();    
    req.open('GET', webApiQuery, false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.send();
    if (req.readyState == 4 /* complete */) {
        if (req.status == 200) {
            var results = JSON.parse(req.response);
            data = results.value[0];
        }
        else {
            var error = JSON.parse(req.response).error;
            console.log(error.message);
        }
    }
    return data;
}


You can filter the statuscode by statecode as following

  var statusReasons = getStatusAttributeMetadata("lead");
    if (statusReasons != null) {
        var options = statusReasons.OptionSet.Options;       
        for (var i = 0; i < options.length; i++) {
            if (options[i].State == 2) {
              //do your code here
            }              
        }
    }

No comments:

Post a Comment