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
1 Answers

Hi,

I think calling renderAsString() before you’ve added the search results might be causing this issue.

Are you getting any information on which line is causing the error?

Thanks,

Chris

Intermediate Answered on September 5, 2023.
Add Comment

Your Answer

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