You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynamics CRM插件创建阶段:预操作阶段如何访问未填充空字段?

Dynamics CRM插件预操作阶段访问未填充字段的方法

在创建流程的preoperation(预操作)阶段,目标实体(Target Entity)的属性集合里只会包含用户明确提交的字段——要么是有值的,要么是被主动设置为空的。未被用户填充的字段不会出现在这个集合里,直接通过Target["field_name"]访问会抛出KeyNotFoundException。不过可以通过以下方式处理这类场景:

  • 检查字段是否存在,自定义空值逻辑
    先通过Contains方法判断字段是否在Target的属性集合中,不存在则视为未填充,执行对应逻辑(比如使用默认值、标记为空等)。示例代码:

    Entity target = (Entity)context.InputParameters["Target"];
    string targetFieldValue = null;
    
    if (target.Contains("your_field_logical_name"))
    {
        targetFieldValue = target.GetAttributeValue<string>("your_field_logical_name");
    }
    else
    {
        // 字段未被用户填充,执行自定义逻辑
        targetFieldValue = "默认值";
    }
    
  • 获取字段的系统默认值
    如果字段在系统元数据中设置了默认值,可以通过组织服务查询实体元数据来获取这个默认值,作为未填充时的 fallback。示例代码:

    // 假设service是IOrganizationService实例
    var retrieveRequest = new RetrieveEntityRequest
    {
        EntityFilters = EntityFilters.Attributes,
        LogicalName = target.LogicalName,
        RetrieveAsIfPublished = true
    };
    var retrieveResponse = (RetrieveEntityResponse)service.Execute(retrieveRequest);
    var entityMetadata = retrieveResponse.EntityMetadata;
    
    // 定位目标字段的元数据
    var attributeMetadata = entityMetadata.Attributes
        .FirstOrDefault(a => a.LogicalName == "your_field_logical_name");
    
    if (attributeMetadata != null)
    {
        // 根据字段类型获取默认值,这里以字符串字段为例
        if (attributeMetadata is StringAttributeMetadata stringAttr)
        {
            var defaultValue = stringAttr.DefaultValue;
            // 使用默认值
        }
    }
    

需要注意的是:预操作阶段无法直接"访问"未被提交的空字段,因为这些字段根本不在Target的属性集合里,只能通过上述两种方式间接处理未填充的场景。

内容的提问来源于stack exchange,提问作者ashkanyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 09:15:37