Dynamics Crm: Get metadata for statuscode/statecode mapping

codeulike picture codeulike · Mar 17, 2013 · Viewed 11.4k times · Source

In Dynamics CRM 2011, on the Incident entity, the "Status Reason" optionset (aka statuscode) is related to the "Status" optionset (aka statecode)

e.g. see this screenshot

screenshot of CRM field options

When I use the API to retrieve the Status Reason optionset, like so:

        RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
        {
            EntityLogicalName = "incident",
            LogicalName = "statuscode",
            RetrieveAsIfPublished = true
        };
        RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)serv.Execute(attributeRequest);
        AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)attrMetadata;
        var dict = new Dictionary<int?, string>();
        foreach (OptionMetadata optionMeta in statusMetadata.OptionSet.Options)
        {
            dict.Add(optionMeta.Value, optionMeta.Label.UserLocalizedLabel.Label);
        }

It works in that I get the whole list of "Status Reason" (statuscode) options. However, I dont get any info about which "Status Reason" (statuscode) options relate to which "Status" (statecode) options.

How do I get that information?

Answer

Pedro Azevedo picture Pedro Azevedo · Mar 18, 2013

You already have everything try insert this code inside of foreach:

 int stateOptionValue = (int)((StatusOptionMetadata)optionMeta).State;

See StatusAttributeMetaData.OptionSet.Options hierarchy can return a type called StatusOptionMetadata if you use the State property of the StatusOptionMetadata, it will return the statecode this statuscode belongs to.