AL中插入多行Item Journal Line时如何获取下一个可用Line No.
如何在Business Central的Item Journal Line中获取下一个可用行号?
你遇到的问题根源是插入多行时重复使用了相同的Line No.值,Item Journal Line表本身没有内置的NextLineNo()函数,但可以通过两种标准方式生成唯一递增的行号:
方法1:手动获取最大行号并递增
通过过滤当前Journal模板和批次的记录,找到已存在的最大行号后,按系统默认步长(1000,方便后续插入中间行)递增:
var ItemJournalLine: Record "Item Journal Line"; NextLineNo: Integer; begin // 定位当前Journal模板和批次的最后一行 ItemJournalLine.SetRange("Journal Template Name", 'AVGANG'); ItemJournalLine.SetRange("Journal Batch Name", 'STANDARD'); if ItemJournalLine.FindLast() then NextLineNo := ItemJournalLine."Line No." + 1000 else NextLineNo := 1000; // 第一行从1000开始,符合系统默认规则 // 初始化并插入新行 ItemJournalLine.Init(); ItemJournalLine."Journal Template Name" := 'AVGANG'; ItemJournalLine."Journal Batch Name" := 'STANDARD'; ItemJournalLine."Line No." := NextLineNo; // 填充其他必填字段(如Item No.、Quantity等) ItemJournalLine.Insert(); end;
方法2:使用标准Journal Management代码单元(推荐)
Business Central内置了Codeunit 39 "Journal Management",其中的GetNewLineNo方法会自动读取Journal模板的行号增量设置,无需硬编码步长,兼容性更强:
var ItemJournalLine: Record "Item Journal Line"; JournalManagement: Codeunit "Journal Management"; NextLineNo: Integer; begin // 设置Journal的模板和批次信息 ItemJournalLine."Journal Template Name" := 'AVGANG'; ItemJournalLine."Journal Batch Name" := 'STANDARD'; // 调用标准函数获取下一个行号 NextLineNo := JournalManagement.GetNewLineNo(ItemJournalLine); // 初始化新行并赋值 ItemJournalLine.Init(); ItemJournalLine."Journal Template Name" := 'AVGANG'; ItemJournalLine."Journal Batch Name" := 'STANDARD'; ItemJournalLine."Line No." := NextLineNo; // 填充其他必要字段 ItemJournalLine.Insert(); end;
注意事项
- 优先使用方法2,它会适配Journal模板中设置的
Line Nos. Increment值(可在Journal Template卡片中修改),避免硬编码步长带来的适配问题。 - 插入多行时,需在每次循环中重新获取下一个行号,确保每一行的
Line No.唯一且递增。
内容的提问来源于stack exchange,提问作者hagru
相关产品推荐
相关产品推荐

