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

Dynamics Create消息同步PostOperation插件无法获取刚创建记录更新价格问题

问题根因
  • 同步模式的Create消息PostOperation阶段,记录写入数据库的操作仍处于未提交的事务中,在当前插件上下文内调用Retrieve查询未提交的记录时,数据库会返回记录不存在的错误。
  • 你的场景无需额外调用Retrieve方法,配置的Post Entity Image已经包含了你需要的价格、记录ID等字段,可以直接使用。
修正方案

最优方案是直接基于Post Image的数据构造更新请求,跳过不必要的检索操作,既避免了事务问题,也提升了插件执行效率,修改后的代码如下:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    // 增加深度判断,防止插件循环触发
    if (context.Depth > 1) return;

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity entity)
    {
        if (entity.LogicalName != "new_books") return;
        
        if (context.PostEntityImages.Contains("newbookpostImage") && context.PostEntityImages["newbookpostImage"] is Entity postMessageEntity)
        {
            decimal bookPrice = ((Money)postMessageEntity["new_price"]).Value;
            Guid recordId = postMessageEntity.Id;
            
            // 仅构造需要更新的字段即可,无需填充全量属性
            Entity updateBook = new Entity("new_books", recordId);
            // 计算加10%GST后的价格
            updateBook["new_price"] = new Money(bookPrice * 1.1m);
            
            // 直接执行更新操作
            service.Update(updateBook);
        }
    }
}

额外说明

如果你的业务场景确实需要完整读取刚创建的记录,可将插件步骤的执行模式从同步修改为异步PostOperation,异步模式下插件会在Create事务提交后执行,此时调用Retrieve即可正常获取到记录。
另外注意确认自定义实体的逻辑名是否正确,你描述中提到的实体名为Library,代码中使用的是new_books,需要保证和系统中实际的实体逻辑名一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 20:15:02