使用SAP B1 DIAPI添加库存转储时触发Internal error (-5002) [الرسالة 131-183]
SAP Business One DIAPI创建库存转储单时Internal error (-5002) [الرسالة 131-183]错误排查与解决
错误场景
使用VB代码通过SAP Business One DIAPI创建库存转储单(StockTransfer)时,触发错误:Internal error (-5002) occurred [الرسالة 131-183],相关代码如下:
Try Dim oST As SAPbobsCOM.StockTransfer = Nothing Dim tmpDt2 As New Date Dim cleanedDateString As String Dim formattedDate As String Dim DCDate As New Date oST = __bobCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oStockTransfer) oST.FromWarehouse = oForm.Items.Item("U_FromWH").Specific.Value & "" oST.ToWarehouse = oForm.Items.Item("U_ToWH").Specific.Value & "" tmpDt2 = DateTime.ParseExact(U_DocDate, GetDateFormat, DateTimeFormatInfo.InvariantInfo) formattedDate = tmpDt2.ToString("yyyyMMdd") Dim serqry As String = "" If oForm.Items.Item("U_FromWH").Specific.Value.ToString().StartsWith("P") Then serqry = "SELECT U_Series FROM [@INVTSER] WHERE U_PostingPeriod = (SELECT Indicator FROM OFPR WHERE '" + tmpDt2 + "' BETWEEN F_RefDate AND T_RefDate) AND U_Branch= '5'" Else serqry = "Select U_Series FROM [@INVTSER] WHERE U_PostingPeriod = (Select Indicator FROM OFPR WHERE '" + formattedDate + "' BETWEEN F_RefDate AND T_RefDate) AND U_Branch= '3'" End If Dim oRSSer As SAPbobsCOM.Recordset oRSSer = __bobCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset) oRSSer.DoQuery(serqry) Try oST.Series = CInt(oRSSer.Fields.Item("U_Series").Value.ToString()) Catch ex As Exception End Try oST.DocDate = tmpDt2 Try oST.Comments = "Based On QC Transfer DocEntry : " & oForm.Items.Item("DocNum").Specific.Value Catch ex As Exception End Try Dim SampQty As Double = 0.0 For iRow As Integer = 1 To mBTQ1.RowCount If mBTQ1.Columns.Item("U_ItemCode").Cells.Item(iRow).Specific.Value & "" <> "" Then If iRow <> 1 Then oST.Lines.Add() End If oST.Lines.SetCurrentLine(iRow - 1) oST.Lines.ItemCode = mBTQ1.Columns.Item("U_ItemCode").Cells.Item(iRow).Specific.Value & "" oST.Lines.WarehouseCode = oForm.Items.Item("U_ToWH").Specific.Value & "" oST.Lines.Quantity = CDbl(mBTQ1.Columns.Item("U_Quantity").Cells.Item(iRow).Specific.Value) oST.Lines.BatchNumbers.Add() oST.Lines.BatchNumbers.SetCurrentLine(0) oST.Lines.BatchNumbers.BatchNumber = mBTQ1.Columns.Item("U_BatchNum").Cells.Item(iRow).Specific.Value oST.Lines.BatchNumbers.Quantity = oST.Lines.Quantity End If Next Dim RetVal As Integer = 0 Dim ErrorCode As Long = 0 Dim ErrorMessage As String = "" RetVal = oST.Add() If RetVal <> 0 Then __bobCompany.GetLastError(ErrorCode, ErrorMessage) __oApplication.MessageBox(ErrorCode & " " & ErrorMessage) oForm.Freeze(False) BubbleEvent = False End If Marshal.ReleaseComObject(oST) oST = Nothing Catch ex As Exception End Try
可能原因及解决办法
1. 日期与会计期间查询异常
- 问题:代码拼接SQL时,一处直接用
tmpDt2(DateTime转字符串),另一处用formattedDate(yyyyMMdd格式),格式不一致可能导致OFPR表查询无结果,进而[@INVTSER]查不到有效Series值;另外DateTime.ParseExact解析失败会导致日期无效,触发转储单日期校验错误。 - 解决:
- 统一SQL日期参数格式,建议用参数化查询避免字符串拼接问题,示例:
serqry = "SELECT U_Series FROM [@INVTSER] WHERE U_PostingPeriod = (SELECT Indicator FROM OFPR WHERE @DocDate BETWEEN F_RefDate AND T_RefDate) AND U_Branch= '5'" oRSSer.Parameters.Item("@DocDate").Value = tmpDt2 - 验证
GetDateFormat返回格式与U_DocDate实际格式完全匹配,添加异常捕获提示解析错误。 - 手动执行
serqry语句,确认是否返回有效Series值,若为空需检查[@INVTSER]表对应期间、分支的配置。
- 统一SQL日期参数格式,建议用参数化查询避免字符串拼接问题,示例:
2. 批次号与库存校验缺失
- 问题:直接设置批次号但未验证该批次在转出仓库的可用库存是否足够,或批次号不存在、已过期,导致系统内部校验失败。
- 解决:
- 添加批次库存校验逻辑,示例:
Dim oRsBatch As SAPbobsCOM.Recordset = __bobCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset) oRsBatch.DoQuery($"SELECT OnHand FROM OIBT WHERE ItemCode='{oST.Lines.ItemCode}' AND WhsCode='{oST.FromWarehouse}' AND BatchNum='{mBTQ1.Columns.Item("U_BatchNum").Cells.Item(iRow).Specific.Value}'") If oRsBatch.RecordCount = 0 Or CDbl(oRsBatch.Fields.Item("OnHand").Value) < oST.Lines.Quantity Then __oApplication.MessageBox($"批次{mBTQ1.Columns.Item("U_BatchNum").Cells.Item(iRow).Specific.Value}库存不足或不存在") Exit Sub End If - 确保批次号无空格或特殊字符,与系统中存在的批次完全一致。
- 添加批次库存校验逻辑,示例:
3. 基础参数有效性问题
- 问题:转出/转入仓库代码无效、物料代码不存在,或物料未启用批次管理但代码中强制添加批次行,触发系统校验错误。
- 解决:
- 校验
U_FromWH和U_ToWH的输入值是否为系统中存在的仓库代码,避免空值或无效值。 - 查询OITM表的
ManageBatchNumbers字段,仅当值为Y时才添加批次行,否则移除BatchNumbers.Add()相关代码。 - 验证
ItemCode是否为系统中存在的有效物料,避免无效物料导致转储失败。
- 校验
4. 凭证系列(Series)配置错误
- 问题:Series值无效、未分配给库存转储单类型,或当前用户无该系列的使用权限,导致单据无法生成。
- 解决:
- 在SAP B1系统中进入【管理】->【系统初始化】->【凭证系列】,确认该Series已分配给库存转储单(StockTransfer)单据类型。
- 检查DIAPI登录用户是否有该凭证系列的使用权限。
- 若查询不到Series值,可设置系统默认的库存转储单Series值作为备选。
5. 异常吞掉导致问题无法定位
- 问题:代码中存在多个空Catch块,隐藏了Series赋值、Comments赋值等环节的错误,无法精准定位问题根源。
- 解决:
- 替换空Catch块,添加错误提示或日志记录,示例:
Try oST.Series = CInt(oRSSer.Fields.Item("U_Series").Value.ToString()) Catch ex As Exception __oApplication.MessageBox("凭证系列赋值错误: " & ex.Message) End Try - 取消注释
ErrorMessage = oST.GetAsXML(),生成XML后在SAP B1中手动导入测试,排查数据是否符合系统要求。
- 替换空Catch块,添加错误提示或日志记录,示例:
6. DIAPI版本兼容问题
- 问题:DIAPI客户端版本与SAP B1服务器版本不一致,导致内部接口调用异常。
- 解决:确保DIAPI版本与SAP B1服务器版本完全匹配,重新安装对应版本的DIAPI组件。
内容的提问来源于stack exchange,提问作者Anupriya Gada
相关产品推荐
相关产品推荐

