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

Visual Studio(C#)中Dynamics 365工单复制创建新工单报错求助

问题描述

我正尝试通过Visual Studio在Dynamics 365中开发插件,实现以下需求:当Bookable Resource Booking的状态设为「Completed」时,复制关联原工单的以下信息到新工单:

  • 工单名称
  • 计费账户
  • 服务账户信息
  • 产品类型
  • 工单类型
  • 价格表详情
  • 已填写的调查信息(若存在)

目前代码中产品类型赋值及工单创建环节出现报错,需要技术帮助。

现有代码
try
{
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        bookableResourceBooking = context.InputParameters["Target"] as Entity;
        brbReference = (EntityReference)bookableResourceBooking.ToEntityReference();
    }

    postImage = (Entity)context.PostEntityImages["PostImage"];
    Guid boo = postImage.GetAttributeValue<Guid>("bookableresourcebookingid");


    bookingStatus = postImage.GetAttributeValue<EntityReference>("bookingstatus").Name;
    if (bookingStatus == "Completed")
    {
        EntityCollection workorder = getWorkorder(service, postImage.GetAttributeValue<EntityReference>("msdyn_workorder").Id.ToString());

        // create a new work order
        msdyn_workorder wo = new msdyn_workorder();
        //String guid = postImage.Id.ToString();
        //guid.Replace("{", "").Replace("}", "");
        //wo.Id = new Guid(guid);

    
     
        wo.msdyn_name = workorder[0].GetAttributeValue<string>("msdyn_name");
        wo.msdyn_BillingAccount = workorder[0].GetAttributeValue<EntityReference>("msdyn_billlingaccount");
        wo.msdyn_ServiceAccount = workorder[0].GetAttributeValue<EntityReference>("msdyn_serviceaccount");
        //wo.new_ProductType = (msdyn_workorder_new_ProductType?)workorder[0].GetAttributeValue<OptionSetValue>("new_productype").Value;
        wo.msdyn_WorkOrderType = workorder[0].GetAttributeValue<EntityReference>("msdyn_workordertype");
        wo.msdyn_PriceList = workorder[0].GetAttributeValue<EntityReference>("msdyn_pricelist");
        service.Create(wo);
问题分析与修复方案

核心问题点

  1. 字段拼写错误:msdyn_billlingaccount多写了一个l,正确字段名应为msdyn_billingaccount,导致计费账户赋值失败。
  2. 产品类型赋值逻辑错误:
    • 注释代码中字段名拼写错误(new_productype → new_producttype)
    • 未做空值检查,原工单未设置产品类型时会抛出空引用异常
  3. 空值安全检查缺失:
    • 未验证workorder集合是否有数据,直接访问workorder[0]会引发索引越界
    • 未检查postImage中的msdyn_workorder是否存在,无关联工单时会报错
  4. 调查信息同步未实现:原代码未处理调查信息的复制逻辑

修复后的代码示例

try
{
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        bookableResourceBooking = context.InputParameters["Target"] as Entity;
        brbReference = bookableResourceBooking.ToEntityReference();
    }

    postImage = context.PostEntityImages["PostImage"] as Entity;
    if (postImage == null) return;

    // 安全获取预订状态名称
    var bookingStatusRef = postImage.GetAttributeValue<EntityReference>("bookingstatus");
    if (bookingStatusRef == null || bookingStatusRef.Name != "Completed") return;

    // 安全获取关联的原工单
    var originalWorkOrderRef = postImage.GetAttributeValue<EntityReference>("msdyn_workorder");
    if (originalWorkOrderRef == null) return;

    EntityCollection workorder = getWorkorder(service, originalWorkOrderRef.Id.ToString());
    if (workorder == null || workorder.Entities.Count == 0) return;

    var originalWo = workorder.Entities[0];

    // 创建新工单
    msdyn_workorder newWo = new msdyn_workorder();

    // 复制基础字段
    newWo.msdyn_name = originalWo.GetAttributeValue<string>("msdyn_name");
    newWo.msdyn_BillingAccount = originalWo.GetAttributeValue<EntityReference>("msdyn_billingaccount"); // 修正拼写错误
    newWo.msdyn_ServiceAccount = originalWo.GetAttributeValue<EntityReference>("msdyn_serviceaccount");
    newWo.msdyn_WorkOrderType = originalWo.GetAttributeValue<EntityReference>("msdyn_workordertype");
    newWo.msdyn_PriceList = originalWo.GetAttributeValue<EntityReference>("msdyn_pricelist");

    // 处理产品类型(OptionSet)
    var productTypeOption = originalWo.GetAttributeValue<OptionSetValue>("new_producttype");
    if (productTypeOption != null)
    {
        newWo.new_ProductType = (msdyn_workorder_new_ProductType)productTypeOption.Value;
    }

    // 同步调查信息(示例:假设调查记录关联到工单的msdyn_surveyresponse字段)
    var surveyResponseRef = originalWo.GetAttributeValue<EntityReference>("msdyn_surveyresponse");
    if (surveyResponseRef != null)
    {
        newWo.msdyn_surveyresponse = surveyResponseRef;
        // 若需复制调查详情而非仅关联引用,需查询调查实体并复制字段
    }

    // 创建新工单
    service.Create(newWo);
}
catch (Exception ex)
{
    // 补充异常处理逻辑,记录错误日志
    throw new InvalidPluginExecutionException("创建新工单失败: " + ex.Message, ex);
}

额外注意事项

  • 确保已正确生成并引用Dynamics 365早期绑定实体类,枚举类型msdyn_workorder_new_ProductType需与系统中字段的选项值匹配
  • getWorkorder方法需确保返回正确的工单数据,建议使用FetchXml或QueryExpression查询,避免直接拼接ID字符串可能引发的格式问题
  • 插件注册时需确保PostImage包含所有需要访问的字段(如bookingstatus、msdyn_workorder等)

内容的提问来源于stack exchange,提问作者M.Awais

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 00:32:49