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

如何在D365 CRM中基于父实体数值创建对应数量的子记录

实现步骤与代码示例

一、明确插件触发规则

  • 触发实体:detail(父实体)
  • 触发事件:根据需求选择两种场景:
    • 场景1:创建父记录时触发 → 选择Create消息
    • 场景2:更新父记录的quantity字段时触发 → 选择Update消息,且仅当quantity字段值变化时执行
  • 执行阶段:必须选Post Operation(确保父记录已成功保存,才能关联子记录)
  • 执行模式:同步(Synchronous)

二、编写C#插件代码

首先需引用Dynamics CRM SDK的Microsoft.Xrm.Sdk程序集,核心代码如下:

using Microsoft.Xrm.Sdk;
using System;

public class GenerateSpotRecords : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // 初始化插件上下文与组织服务
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);

        // 校验目标实体与操作有效性
        if (context.PrimaryEntityName != "detail" || !context.InputParameters.Contains("Target"))
            return;

        Entity targetDetail = (Entity)context.InputParameters["Target"];
        int targetQuantity = 0;

        // 获取quantity字段值
        if (targetDetail.Contains("quantity"))
        {
            targetQuantity = targetDetail.GetAttributeValue<int>("quantity");
        }

        // Update事件额外校验:仅当quantity值变化时执行
        if (context.MessageName.Equals("Update", StringComparison.OrdinalIgnoreCase))
        {
            if (!context.PreEntityImages.Contains("PreImage")) return;
            Entity preDetail = context.PreEntityImages["PreImage"];
            int oldQuantity = preDetail.GetAttributeValue<int>("quantity");
            if (targetQuantity == oldQuantity) return;
        }

        // 循环创建spot子记录
        if (targetQuantity <= 0) return;
        for (int i = 0; i < targetQuantity; i++)
        {
            Entity spotRecord = new Entity("spot");
            // 设置父实体关联的lookup字段
            spotRecord["detail(parent)"] = new EntityReference("detail", targetDetail.Id);
            // 可选:设置spot的其他字段,比如名称
            spotRecord["name"] = $"Spot {i + 1}";

            orgService.Create(spotRecord);
        }
    }
}

代码关键点说明

  • 上下文与服务初始化:这是所有Dynamics CRM插件的基础,用于获取操作上下文和执行数据操作
  • 事件校验:避免插件在无关实体/操作上触发,减少资源消耗
  • Update事件的预镜像(PreImage):用于对比quantity字段的新旧值,防止无意义重复执行
  • 子记录创建:通过EntityReference关联父实体,确保每条spot记录都指向正确的detail父记录

三、插件注册配置

使用Plugin Registration Tool完成注册:

  1. 连接到你的CRM环境,导入编译好的插件程序集
  2. 为detail实体添加插件步骤:
    • 若为Create事件:选择消息Create,阶段Post Operation,执行模式Synchronous
    • 若为Update事件:选择消息Update,阶段Post Operation,执行模式Synchronous,并设置Filtering Attributes为quantity(仅当该字段修改时触发)
  3. 确保插件执行的上下文用户拥有创建spot记录、读写detail记录的权限

四、测试验证

  • 场景1:创建一条detail记录,设置quantity=5,检查系统是否自动生成5条关联的spot记录
  • 场景2:修改已存在detail记录的quantity值为3,检查是否新增3条spot记录(若需求是替换原有子记录,需额外添加删除旧spot的逻辑)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 10:29:54