Reputation: 107
The Problem: I need to create a dummy packing slip from an open sales order for drop-ship customers.
Step 1: I've made a user-event button that calls this client-script click handler:
define(['N/url', 'N/currentRecord'], function (url, currentRecord) {
function pageInit(context) {
}
function onButtonClick() {
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_zg_drop_packingslip_pdf', // my suitelet script id
deploymentId: 'customdeploy_zg_drop_packingslip_pdf', // my suitelet deployment id
returnExternalUrl: false,
params: {
custom_id: currentRecord.get().id,
},
});
window.open(suiteletUrl);
}
exports.onButtonClick = onButtonClick;
exports.pageInit = pageInit;
return exports;
});
Step 2: I'm stuck on the suitelet. I want it to pull data from the current record and pass it through an Advanced PDF template to create my drop-ship packing slip in a new window.
define(['N/render', 'N/record', 'N/xml'],
function(render, record, xml) {
function onRequest(context) {
var id = context.request.parameters.custom_id;
if (!id) {
context.response.write('The parameter "custom_id" is required');
return;
}
var renderer = render.create(id);
renderer.setTemplateByScriptId("CUSTTMPL_128_3768700_732"); // The ID of my Packing Slip Advanced PDF Template
var myPdf = context.response.write(renderer.templateContent);
}
return {
onRequest:onRequest
}
})
The current output is not showing up as a pdf, it stretches and scales with the browser window. And instead of passing my current record data through it, I only get the schema-browser token ids. It currently looks like this:
But I want it to look like this (as my Advanced PDF Template actually looks), with real data from the current sales order, and as an in-browser pdf which I can save, print, and email. I do not need to create or store these files in the file cabinet:
How do I get my suitelet to pull the open order data and render it as a pdf from my template?
Upvotes: 0
Views: 8077
Reputation: 91
You can use the below to render as PDF
var renderer = render.create();
renderer.setTemplateByScriptId('CUSTTMPL_734_5415577_141');
renderer.addRecord({
templateName: 'record',
record: record.load({
type: 'invoice',
id: id
})
});
var pdfFile = render.xmlToPdf({
xmlString: renderer.renderAsString()
});
context.response.writeFile(pdfFile, true);
Upvotes: 0
Reputation: 1078
When you want to generate a PDF file, you must use:
context.response.writeFile(renderer.renderAsPdf());
Also the renderer.create function doesn't accept an "id" : you will find good examples on how to generate the PDF file here: https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4412042824.html
An example is :
var renderer = render.create();
renderer.addRecord('saleorder', record.load({
type: record.Type.SALES_ORDER,
id: context.request.parameters.custom_id
}));
renderer.setTemplateByScriptId("CUSTTMPL_128_3768700_732");
context.response.writeFile(renderer.renderAsPdf());
Upvotes: 4
Reputation: 2850
Based on the Help docs, render.create does not expect the id parameter.
Try the following
var renderer = render.create();
renderer.addRecord({
templateName: 'record',
record: record.load({
type: <recordtype>,
id: <recordid>
});
});
var invoicePdf = renderer.renderAsPdf();
Upvotes: 0