如何使用SuiteScript 2.0删除Suitelet子列表中选中的行?
NetSuite Suitelet子列表移除选中行失败问题解决
问题场景
我创建了自定义Suitelet,在子列表中添加了复选框和供应商账单数据。勾选供应商账单对应的复选框并点击按钮后,希望移除选中的行,但使用removeLine API未能成功实现。以下是提交按钮调用的客户端脚本函数:
function CallforSuitelet(context) { var billArray=[]; var tempFlag="F"; var currentRec = currentRecord.get(); var lineCnt = currentRec.getLineCount({ sublistId: 'custpage_openbills_sublist' }); for(var sublist=0;sublist< lineCnt;sublist++) { var checkbox=currentRec.getSublistValue({ sublistId: 'custpage_openbills_sublist', fieldId: 'custpage_select', line: sublist }); console.log("checkbox",checkbox); if(checkbox==true) { var billId=currentRec.getSublistValue({ sublistId: 'custpage_openbills_sublist', fieldId: 'custpage_internalid', line: sublist }); billArray.push(billId); alert("sublist line number="+sublist); currentRec.removeLine({ sublistId: 'custpage_openbills_sublist', line: sublist }); } }//sublist close var billArrays=JSON.stringify(billArray); console.log("billArrays",billArrays); var suiteurl = url.resolveScript({ deploymentId: "customdeploy_sut_create_filter_suitelet", scriptId: "customscript_sut_create_filter_suitelet", params: { 'billarrays': billArrays } }); console.log("suiteurl",suiteurl); window.open(suiteurl, "_self"); }//function close
问题原因
核心问题是正向循环导致的索引错位:
当从第0行开始正向遍历并移除行时,移除某一行后,后面所有行的索引会自动减1。比如移除第2行后,原来的第3行变成新的第2行,但循环计数器会继续递增到3,直接跳过了新的第2行,导致部分选中的行无法被移除。
解决方案
将循环改为倒序遍历(从最后一行往前遍历),这样移除行不会影响未遍历的行索引。同时优化removeLine调用参数,添加ignoreRecalc: true避免不必要的重计算,提升执行效率。
修正后的代码:
function CallforSuitelet(context) { var billArray = []; var currentRec = currentRecord.get(); var lineCnt = currentRec.getLineCount({ sublistId: 'custpage_openbills_sublist' }); // 倒序遍历子列表,避免移除行后索引错位 for (var sublist = lineCnt - 1; sublist >= 0; sublist--) { var checkbox = currentRec.getSublistValue({ sublistId: 'custpage_openbills_sublist', fieldId: 'custpage_select', line: sublist }); console.log("checkbox", checkbox); if (checkbox === true) { var billId = currentRec.getSublistValue({ sublistId: 'custpage_openbills_sublist', fieldId: 'custpage_internalid', line: sublist }); billArray.push(billId); console.log("移除子列表行号=" + sublist); // 添加ignoreRecalc参数,减少不必要的重计算 currentRec.removeLine({ sublistId: 'custpage_openbills_sublist', line: sublist, ignoreRecalc: true }); } } var billArrays = JSON.stringify(billArray); console.log("billArrays", billArrays); var suiteurl = url.resolveScript({ deploymentId: "customdeploy_sut_create_filter_suitelet", scriptId: "customscript_sut_create_filter_suitelet", params: { 'billarrays': billArrays } }); console.log("suiteurl", suiteurl); window.open(suiteurl, "_self"); }
额外说明
- 倒序遍历:从最后一行开始检查,即使移除当前行,前面的行索引不会改变,确保所有选中的行都能被处理。
ignoreRecalc参数:设置为true可以避免每次移除行后重新计算子列表的总计或其他依赖字段,提升脚本执行速度,尤其是子列表行数较多时。- 如果跳转后页面仍显示已移除的行,需确保Suitelet的服务器端逻辑接收
billarrays参数后,重新加载数据时排除这些已选中的账单ID。
内容的提问来源于stack exchange,提问作者Maira S
相关产品推荐
相关产品推荐

