Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamics Crm: Get metadata for statuscode/statecode mapping

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?

like image 283
codeulike Avatar asked Mar 17 '13 17:03

codeulike


People also ask

What is Principlalobjectaccess table why is it used Mscrm?

POA table stores information about sharing of records in CRM. Every time shares are granted on records in CRM, a new entry is created in POA table. It stores information of User/Team with the record is stored, Object GUID, rights granted, etc.

What is the use of string map table in Mscrm?

String map table used in MSCRM for storing the details of Option Set Fields exists in an organization . It contains all the data (Attribute Name, OptionSet name, option value , option name) of option set.

What is Statecode in CRM?

statecode. Status. Represents the state of the record. For custom entities this is Active or Inactive. The Incident (case) entity uses Active, Resolved, and Canceled.


2 Answers

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.

like image 100
Pedro Azevedo Avatar answered Oct 23 '22 10:10

Pedro Azevedo


Here is working code that will output State/Status mapping for a given entity (you just need to provide the orgServiceProxy):

    var dictState = new Dictionary<int, OptionMetadata>();
    var dictStatus = new Dictionary<int, List<OptionMetadata>>();

    string entityName = "lead";
    int count=0;
    using (var orgServiceProxy = GetOrgServiceProxy(orgServiceUriOnLine))
    {
        RetrieveAttributeResponse attributeResponse = GetAttributeData(orgServiceProxy, entityName, "statecode");
        AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StateAttributeMetadata stateMetadata = (StateAttributeMetadata)attrMetadata;
        foreach (OptionMetadata optionMeta in stateMetadata.OptionSet.Options)
        {
            dictState.Add(optionMeta.Value.Value,optionMeta);
            dictStatus.Add(optionMeta.Value.Value,new List<OptionMetadata>());
        }

        attributeResponse = GetAttributeData(orgServiceProxy, entityName, "statuscode");
        attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
        StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)attrMetadata;

        foreach (OptionMetadata optionMeta in statusMetadata.OptionSet.Options)
        {
            int stateOptionValue = ((StatusOptionMetadata)optionMeta).State.Value;
            var statusList = dictStatus[stateOptionValue];
            statusList.Add(optionMeta);
            count++;
        }
    }
    Console.WriteLine($"Number of mappings: {count}");
    foreach (var stateKvp in dictState.OrderBy(x=> x.Key))
    {
        Console.WriteLine($"State: {stateKvp.Value.Value}: {stateKvp.Value.Label.UserLocalizedLabel.Label}");
        var statusList = dictStatus[stateKvp.Key];
        Console.WriteLine($"\tStatuses");
        foreach (var status in statusList.OrderBy(s => s.Value))
        {
            Console.WriteLine($"\t\t{stateKvp.Value.Value}: {status.Label.UserLocalizedLabel.Label}");
        }
    }
like image 28
Raj Rao Avatar answered Oct 23 '22 11:10

Raj Rao