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

Dynamics CRM插件开发:Entity转Contact早期绑定类后值为空求助

Dynamics CRM插件早期绑定类转换后值为null的问题排查与解决

可能的原因及解决方法:

  • 消息类型仅传递修改字段
    插件触发的Update消息默认只携带被修改的字段,而非整个实体。你看到的targetEntity里只有用户修改过的属性,转成Contact后未修改的属性自然是null。可以通过遍历targetEntity.Attributes查看实际存在的字段。
    解决:如果需要完整实体,要么在插件注册时勾选「传递所有属性」(仅部分消息支持),要么用IOrganizationService.Retrieve从数据库拉取完整记录。

  • 早期绑定类元数据不匹配
    生成早期绑定类时如果元数据过时,或者自定义字段的命名映射错误,会导致转换时无法匹配属性。比如CRM中的字段逻辑名是new_customfield,但生成的类里字段名是NewCustomField(大小写或命名规则不符)。
    解决:重新生成早期绑定类,确保使用最新的CRM元数据,生成命令示例:

    CrmSvcUtil.exe /url:https://yourorg.crm.dynamics.com/XRMServices/2011/Organization.svc /out:EarlyBound.cs /namespace:YourOrg.EarlyBound /username:user@yourorg.onmicrosoft.com /password:yourpassword
    
  • 转换方式的潜在问题
    ToEntity<T>()是标准转换方法,但如果实体元数据缓存异常,可能导致转换失败。可以尝试直接强制转换,若抛出异常能快速定位问题:

    Contact updatingContact = (Contact)targetEntity;
    

    解决:如果强制转换抛出InvalidCastException,说明实体类型不匹配或元数据有问题;若成功,再检查targetEntity的属性集合确认字段是否存在。

修正后的示例代码:

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity targetEntity)
{
    if (targetEntity.LogicalName != Contact.EntityLogicalName)
        return;

    // 先确认实体包含的属性
    foreach (var attr in targetEntity.Attributes)
    {
        Trace.WriteLine($"属性: {attr.Key}, 值: {attr.Value}");
    }

    // 尝试强制转换
    Contact updatingContact;
    try
    {
        updatingContact = (Contact)targetEntity;
    }
    catch (InvalidCastException ex)
    {
        Trace.WriteLine($"转换失败: {ex.Message}");
        return;
    }

    // 如需完整实体,调用Retrieve获取
    if (context.PrimaryEntityId != Guid.Empty && service != null)
    {
        var fullContact = service.Retrieve(Contact.EntityLogicalName, context.PrimaryEntityId, new ColumnSet(true));
        updatingContact = fullContact.ToEntity<Contact>();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 03:02:45