NetSuite脚本报错:运行已保存搜索渲染PDF模板时参数缺失
问题描述
需创建已保存搜索(saved search)及配套的高级PDF模板,编写脚本运行该搜索并渲染自定义PDF模板。目前已保存搜索可正常加载,但无法将搜索结果填充至模板,报错信息为Missing parameters required to generate PDF,请求协助指引正确方向。
相关脚本
/** * @NApiVersion 2.x * @NScriptType ScheduledScript */ require(['N/search', 'N/render', 'N/email', 'N/file'], function(search, render, email, file) { function runSearchAndFetchResult() { try { var mySearch = search.load({ id: 2825 }); var searchResult = mySearch.run().getRange({ start: 0, end: 10 }); log.debug('searchResult', searchResult); var renderer = render.create(); renderer.templateContent = renderer.setTemplateByScriptId("custtmpl_nscs_bin_label"); renderer.templateContent = renderer.renderAsString();; renderer.addSearchResults({ templateName: 'results', searchResult: searchResult }); var newfile = renderer.renderAsPdf(); newfile.name = 'Testpdf2.pdf'; newfile.folder = 2754; // Save the file newfile.save(); } catch (e) { log.error({ title: 'Error', details: 'An error occurred: ' + e.message }); } } runSearchAndFetchResult(); });
高级PDF模板代码
<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd"> <pdf> <head> <link name="NotoSans" type="font" subtype="truetype" src="${nsfont.NotoSans_Regular}" src-bold="${nsfont.NotoSans_Bold}" src-italic="${nsfont.NotoSans_Italic}" src-bolditalic="${nsfont.NotoSans_BoldItalic}" bytes="2" /> <#if .locale == "zh_CN"> <link name="NotoSansCJKsc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKsc_Regular}" src-bold="${nsfont.NotoSansCJKsc_Bold}" bytes="2" /> <#elseif .locale == "zh_TW"> <link name="NotoSansCJKtc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKtc_Regular}" src-bold="${nsfont.NotoSansCJKtc_Bold}" bytes="2" /> <#elseif .locale == "ja_JP"> <link name="NotoSansCJKjp" type="font" subtype="opentype" src="${nsfont.NotoSansCJKjp_Regular}" src-bold="${nsfont.NotoSansCJKjp_Bold}" bytes="2" /> <#elseif .locale == "ko_KR"> <link name="NotoSansCJKkr" type="font" subtype="opentype" src="${nsfont.NotoSansCJKkr_Regular}" src-bold="${nsfont.NotoSansCJKkr_Bold}" bytes="2" /> <#elseif .locale == "th_TH"> <link name="NotoSansThai" type="font" subtype="opentype" src="${nsfont.NotoSansThai_Regular}" src-bold="${nsfont.NotoSansThai_Bold}" bytes="2" /> </#if> <style type="text/css">table { font-size: 9pt; table-layout: fixed; width: 100%; } th { font-weight: bold; font-size: 8pt; vertical-align: middle; padding: 5px 6px 3px; background-color: #e3e3e3; color: #333333; padding-bottom: 10px; padding-top: 10px; } td { padding: 4px 6px; } b { font-weight: bold; color: #333335; } </style> </head> <body margin-left="-35" margin-top="-35" size="2.5in x 1in"> <#list results as result> <table float="left"> <tr> <td colspan="3" style='font-weight:bold; font-size: 14pt;'> ${result.binnumber}</td> </tr> <#if result?has_next> <tr> <td colspan="3"><barcode codetype="code128" showtext="false" value="${result.binnumber}"></barcode></td> </tr></table> <pbr /> <#else> <tr> <td colspan="3"><barcode codetype="code128" showtext="false" value="${result.binnumber}"></barcode></td> </tr></table> </#if> </#list> </body> </pdf>
解决方案
脚本存在两处关键错误,导致无法正确传递参数到PDF模板:
- 错误调用
setTemplateByScriptId:该方法无返回值,不能将其赋值给renderer.templateContent,应直接调用。 - 错误提前调用
renderAsString:这行代码会在未添加搜索结果的情况下渲染模板,且覆盖了之前设置的模板内容,必须删除。
修正后的脚本:
/** * @NApiVersion 2.x * @NScriptType ScheduledScript */ require(['N/search', 'N/render', 'N/email', 'N/file'], function(search, render, email, file) { function runSearchAndFetchResult() { try { var mySearch = search.load({ id: 2825 }); var searchResult = mySearch.run().getRange({ start: 0, end: 10 }); log.debug('searchResult', searchResult); var renderer = render.create(); // 直接调用setTemplateByScriptId,无需赋值 renderer.setTemplateByScriptId("custtmpl_nscs_bin_label"); // 添加搜索结果到模板 renderer.addSearchResults({ templateName: 'results', searchResult: searchResult }); var newfile = renderer.renderAsPdf(); newfile.name = 'Testpdf2.pdf'; newfile.folder = 2754; // Save the file newfile.save(); } catch (e) { log.error({ title: 'Error', details: 'An error occurred: ' + e.message }); } } runSearchAndFetchResult(); });
额外检查点:
- 确认已保存搜索的结果中包含
binnumber字段,确保模板中${result.binnumber}能正确取值。 - 验证高级PDF模板的设置中,已正确关联对应的记录类型或已保存搜索。
内容的提问来源于stack exchange,提问作者bill_meister
相关产品推荐
相关产品推荐

