RE: Script to Run Saved Search and Render in PDF Template

I have a use case where we need to create a saved search and an advanced PDF template to go with it. I need a script to run the search and render the custom PDF template I made. It seems like this is all possible. I have it so the saved search loads fine. I can’t get the search to fill in the template though. Error says: “Missing parameters required to generate PDF”.  Can someone help me get this going in the right direction?

The script:

/**

* @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();

});

The Advance PDF template

<?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;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${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>

RE: Script to Run Saved Search and Render in PDF Template

bill_meister Rookie Asked on August 29, 2023 in SuiteScript.
Add Comment
2 Answers

The error is pointing at the wrong line — renderAsPdf() is where it surfaces, but the template was already lost three lines earlier. Three fixes:

First, setTemplateByScriptId returns void. This line loads your template and then immediately overwrites it with undefined:

renderer.templateContent = renderer.setTemplateByScriptId("custtmpl_nscs_bin_label");

Don’t assign it — just call it.

Second, the next line renders the template before any search results are bound, then stuffs that output back in as the new template. Delete it entirely — you only render once, at the end, after the data is added.

Third, the sneaky one: setTemplateByScriptId requires the script ID in UPPERCASE. Pass custtmpl_... lowercase and it fails silently — no error at that line — and the failure only shows up later at renderAsPdf() as MISSING_PDF_PARAMETERS.

Working version of that section:

var renderer = render.create();
renderer.setTemplateByScriptId("CUSTTMPL_NSCS_BIN_LABEL");
renderer.addSearchResults({
    templateName: 'results',
    searchResult: searchResult
});
var newfile = renderer.renderAsPdf();

Your template’s fine — <#list results as result> matches templateName: 'results', so once the renderer actually has the template and the data in that order, it should produce your bin labels.

One structural note: a 2.x scheduled script should use define() returning an {execute: ...} entry point rather than require() with an immediately-invoked function — worth fixing while you’re in there.

The “silent failure upstream, error downstream” pattern here is common enough with this API that I wrote it up: https://nesystemsgroup.com/tips/missing-pdf-parameters-settemplatebyscriptid

Rookie Answered on August 19, 2026.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.