【问题标题】:Dynamics CRM - Accessing Custom Product Option ValueDynamics CRM - 访问自定义产品选项值
【发布时间】:2013-04-23 03:39:59
【问题描述】:

请问有没有办法以编程方式访问已在 MS CRM Dynamics 中创建为自定义字段的标签和值字段?

我添加了一个名为“new_producttypesubcode”的自定义字段,例如,它有 2 个选项,Trophy = 1000000 和 Kit = 10000001。

我正在编写一个导入实用程序,用于在客户网站和他们的 CRM 之间镜像产品,我想获取 CRM 中所有可能的产品选项的列表,以查看它们是否在网站中匹配。

所以,本质上我想...

  1. 获取可能的 new_producttype 子代码及其对应值的列表。
  2. 遍历网站中的产品变体。
  3. 如果产品变体名称与 new_producttypecodes 列表中的任何名称匹配,则添加值 1000000

所以,如果我发现一个产品添加到网站并且其标记为“奖杯”并且 CRM 中存在“奖杯”,那么 new OptionSetValue(100000001)

我希望这是有道理的......

谢谢

【问题讨论】:

  • 选项集是全局的还是本地的?

标签: dynamics-crm-2011


【解决方案1】:

是的,这些数据都存储在属性的元数据中 (SDK article)。您必须检索实体的实体元数据,然后在列表中找到该属性。然后将该属性转换为 PicklistAttributeMetadata 对象,它将包含一个选项列表。我会提到,通常从 CRM 中检索元数据是一项昂贵的操作,因此请考虑缓存。

private static OptionSetMetadata RetrieveOptionSet(IOrganizationService orgService,
    string entityName, string attributeName)
{
    var entityResponse = (RetrieveEntityResponse)orgService.Execute(
        new RetrieveEntityRequest 
            { LogicalName = entityName, EntityFilters = EntityFilters.Attributes });

    var entityMetadata = entityResponse.EntityMetadata;

    for (int i = 0; i < entityMetadata.Attributes.Length; i++)
    {
        if (attributeName.Equals(entityMetadata.Attributes[i].LogicalName))
        {
            if (entityMetadata.Attributes[i].AttributeType.Value == 
                    AttributeTypeCode.Picklist)
            {
                var attributeMD = (PicklistAttributeMetadata)
                    entityMetadata.Attributes[i];

                return attributeMD.OptionSet;
            }
        }
    }

    return null;
}

以下是如何使用上述调用将选项写入控制台。

var optionSetMD = RetrieveOptionSet(orgService, "account", "accountcategorycode");
var options = optionSetMD.Options;

for (int i = 0; i < options.Count; i++)
{
    Console.WriteLine("Local Label: {0}.  Value: {1}",
        options[i].Label.UserLocalizedLabel.Label,
        options[i].Value.HasValue ? options[i].Value.Value.ToString() : "null");
}

我相信这也适用于全局选项集属性,但如果您知道它是一个全局选项集,则会有不同的消息可能会更有效 (SDK article)。

【讨论】:

  • 对于全局选项集必须使用 RetrieveOptionSetRequest 类,对于本​​地选项集也可以使用 RetrieveAttributeRequest 类。另一件事是,您只检查代码中的一种语言,最好通过 LocalizedLabels 属性检查所有标签。
  • RetrieveAttributeRequest 会更合适。我忘记了。但是我刚刚验证了具有全局选项集的属性在上面的代码中有效。
  • 是的,你是对的,RetrieveOptionSetRequest 对于全局选项集不是强制性的,抱歉我的不准确,因为我一直使用该类作为全局选项集。
【解决方案2】:

此函数检索本地化给当前用户的可能值的字典。取自:CRM 2011 Programatically Finding the Values of Picklists, Optionsets, Statecode, Statuscode and Boolean (Two Options)

static Dictionary<String, int> GetNumericValues(IOrganizationService service, String entity, String attribute)
{
    RetrieveAttributeRequest request = new RetrieveAttributeRequest
    {
        EntityLogicalName = entity,
        LogicalName = attribute,
        RetrieveAsIfPublished = true
    };

    RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(request);

    switch (response.AttributeMetadata.AttributeType)
    {
        case AttributeTypeCode.Picklist:
        case AttributeTypeCode.State:
        case AttributeTypeCode.Status:
            return ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options
                .ToDictionary(key => key.Label.UserLocalizedLabel.Label, option => option.Value.Value);

        case AttributeTypeCode.Boolean:
            Dictionary<String, int> values = new Dictionary<String, int>();

            BooleanOptionSetMetadata metaData = ((BooleanAttributeMetadata)response.AttributeMetadata).OptionSet;

            values[metaData.TrueOption.Label.UserLocalizedLabel.Label] = metaData.TrueOption.Value.Value;
            values[metaData.FalseOption.Label.UserLocalizedLabel.Label] = metaData.FalseOption.Value.Value;

            return values;

        default:
            throw new ArgumentOutOfRangeException();
    }
}

因此,您需要执行以下操作:

Dictionary<String, int> values = GetNumericValues(proxy, "your_entity", "new_producttypesubcode");

if(values.ContainsKey("Trophy"))
{
    //Do something with the value
    OptionSetValue optionSetValue = values["Trophy"];
    int value = optionSetValue.Value;
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多