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

SuiteScript子列表操作报错:如何通过lineKey判断是否为新行?

解决方案

判断子列表行是否为新行

有两种可靠方法可通过lineKey判断某行是否为新行:

  • 检查主键字段值:新行未保存,主键字段(默认id字段,自定义子列表取对应主键字段)为空,现有行的主键会有有效值。示例代码:
var isNewLine = !currentRecord.getSublistValue({
    sublistId: "custpage_sublist1",
    fieldId: "id",
    line: lineKey
});
  • 对比行索引与行总数:现有行的索引范围是0到行总数-1,新行的索引等于当前子列表的行总数。示例代码:
var lineCount = currentRecord.getLineCount({sublistId: "custpage_sublist1"});
var isNewLine = (lineKey === lineCount);

解决新行设置字段值的报错问题

客户端脚本中currentRecord对象确实没有setSublistValue方法,必须通过选中行后调用setCurrentSublistValue。针对新行和现有行需分别处理选中逻辑:

var record = currentRecord.get();    
var sublistId = "custpage_sublist1";
var lineCount = record.getLineCount({sublistId: sublistId});
var isNewLine = (lineKey === lineCount);

// 区分新行与现有行,选择对应选中方法
if (isNewLine) {
    record.selectNewLine({sublistId: sublistId});
} else {
    record.selectLine({sublistId: sublistId, line: lineKey});
}

// 设置字段值并提交行修改
record.setCurrentSublistValue({
    sublistId: sublistId,
    fieldId: 'custcol_2',  
    value: "TEST", 
    ignoreFieldChange: true
});
record.commitLine({sublistId: sublistId});

注意:修改子列表行后必须调用commitLine,否则修改不会生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 00:10:33