13

在 Dynamics CRM 2011 中,在事件实体上,“状态原因”选项集(又名状态代码)与“状态”选项集(又名状态代码)相关

例如看这个截图

CRM 字段选项的屏幕截图

当我使用 API 检索状态原因选项集时,如下所示:

        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);
        }

它的工作原理是我得到了“状态原因”(状态代码)选项的整个列表。但是,我没有得到关于哪些“状态原因”(状态代码)选项与哪些“状态”(状态代码)选项相关的任何信息。

我如何获得这些信息?

4

3 回答 3

14

您已经完成了一切尝试在 foreach 中插入此代码:

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

请参见StatusAttributeMetaData.OptionSet.Options层次结构可以返回一个称为StatusOptionMetadata的类型,如果您使用 StatusOptionMetadata 的 State 属性,它将返回此状态码所属的状态码。

于 2013-03-18T10:11:46.377 回答
6

以下是如何通过查询数据库来获取它

SELECT distinct e.LogicalName as entity,
      smState.Value AS stateCode,
      smstate.AttributeValue,
      smStatus.Value AS [statuscode/statusreason],
      smStatus.AttributeValue
FROM StatusMap sm
     JOIN Entity e ON sm.ObjectTypeCode = e.ObjectTypeCode
     JOIN StringMap smState ON smState.AttributeValue = sm.State
                           AND smState.ObjectTypeCode = e.ObjectTypeCode
                           AND smState.AttributeName = 'StateCode'
     JOIN StringMap smStatus ON smStatus.AttributeValue = sm.Status
                            AND smStatus.ObjectTypeCode = e.ObjectTypeCode
                            AND smStatus.AttributeName = 'StatusCode'
WHERE  e.LogicalName in ('lead')
ORDER BY e.LogicalName,
      smState.AttributeValue,
      smStatus.AttributeValue;
于 2017-04-08T18:02:03.947 回答
1

这是将为给定实体输出状态/状态映射的工作代码(您只需要提供 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}");
        }
    }
于 2017-04-08T18:57:59.767 回答