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

Dynamics 365 BC Cloud Xmlport异常:捕获返回值时导入失败

Dynamics 365 Business Central Xmlport.Import返回值异常问题

在Dynamics 365 Business Central Cloud环境中使用Xmlport导入数据时,出现了特殊的“薛定谔”异常:

  • 捕获Xmlport.Import返回值并用if判断时,返回值始终为False,数据完全无法提交
  • 直接调用Xmlport.Import不捕获返回值时,导入流程正常无报错,数据能正确入库

两种场景下参数、Xmlport配置完全一致,未使用全局变量,仅存在是否捕获返回值的差异。由于是自动队列处理场景,必须捕获返回值避免未处理错误终止进程,因此需要解决该问题。

异常代码(捕获返回值)

tempBlob.CreateInStream(inStreamObj);
if Xmlport.Import(idXmlport, inStreamObj) then begin
    // 从未进入此分支
    exit(true);
end else begin
    // 始终返回False,无数据导入
    exit(false);
end;

正常代码(不捕获返回值)

tempBlob.CreateInStream(inStreamObj);
Xmlport.Import(codigoXmlport, inStreamObj); // 运行正常
exit(true);

可能的原因

  1. 事务提交逻辑差异
    直接调用Xmlport.Import时,系统会隐式完成事务提交;但用if判断返回值时,Xmlport内部的事务可能因逻辑分支的影响未触发提交,甚至被回滚。

  2. Xmlport内部警告被判定为失败
    Xmlport可能存在未抛出的警告级错误,不捕获返回值时系统忽略警告继续导入;捕获返回值时,这些警告会被判定为导入失败,返回False并回滚数据。

  3. 流对象状态异常
    在if条件中调用Xmlport.Import时,流对象inStreamObj的读取位置可能出现偏移,导致Xmlport无法正确读取数据,从而返回False。

解决方案

方案1:显式控制事务

手动管理事务生命周期,确保导入完成后根据结果提交或回滚:

tempBlob.CreateInStream(inStreamObj);
Commit(); // 提交之前的未完成事务
Xmlport.Import(idXmlport, inStreamObj);
if GetLastErrorText() = '' then begin
    Commit();
    exit(true);
end else begin
    Rollback();
    Message('导入失败:%1', GetLastErrorText());
    exit(false);
end;

方案2:重置流对象状态

避免流在条件判断中被异常消耗,通过复制Blob重置流的读取位置再调用:

var
    tempBlobCopy : Codeunit "Temp Blob";
begin
    // 复制原Blob到临时对象,确保流位置初始化为起始点
    tempBlobCopy.Copy(tempBlob);
    tempBlobCopy.CreateInStream(inStreamObj);
    if Xmlport.Import(idXmlport, inStreamObj) then begin
        exit(true);
    end else begin
        Message('导入错误详情:%1', GetLastErrorText());
        exit(false);
    end;
end;

方案3:检查Xmlport错误处理配置

打开Xmlport设计界面,调整错误处理设置:

  • 将“错误处理”选项设置为“继续”或“跳过记录”,而非“终止”
  • 排查Xmlport内的验证逻辑,确认是否存在在捕获返回值时触发失败的判定规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 18:30:17