Enhancing Advanced PDF Templates with Suitelet Scripts in NetSuite
As a NetSuite developer, you might often find yourself needing to insert Suitelet scripts into Advanced PDF templates. This process allows you to generate dynamic content and customize the PDF output according to your business requirements. In this blog post, we will walk you through the steps of inserting a Suitelet script into an Advanced PDF template with the necessary code examples.
Step 1: Create a Suitelet Script
First, you need to create a Suitelet script that will generate the data needed for your Advanced PDF template. In this example, we'll create a simple Suitelet that retrieves data from a Sales Order record.
<---javascript-->
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/record', 'N/search'], function (record, search) {
function onRequest(context) {
if (context.request.method === 'GET') {
var salesOrderId = context.request.parameters.soId;
var salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: salesOrderId
});
var itemLines = [];
' }); i++) {
var item = {
name: salesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'item_display',
line: i
}),
quantity: salesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
}),
amount: salesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
})
};
itemLines.push(item);
}
context.response.write(JSON.stringify(itemLines));
}
}
return {
onRequest: onRequest
};
});
Step 2: Deploy the Suitelet
After creating the Suitelet script, you need to deploy it in NetSuite. Go to **Customization > Scripting > Scripts > New** and select the script file you just created. After uploading the script file, click **Deploy Script** to deploy the Suitelet.
Step 3: Create the Advanced PDF Template
Now, create an Advanced PDF template that will call the Suitelet script to get the data. In this example, we will use the Sales Order record to generate the PDF.
Go to **Customization > Forms> Advanced PDF/HTML Templates > New Template** and select the Sales Order record type. In the template editor, insert the following XML and FreeMarker code:
<---xml--->
<?xml version="1.0"?>
<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<head>
<style type="text/css">/* Add your custom CSS here */</style>
</head>
<body>
<h1>Sales Order: ${record.tranid}</h1>
<p>Date: ${record.trandate}</p>
<p>Customer: ${record.entity}</p>
<table>
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<#assign suiteletURL = 'https://<ACCOUNT_ID>.suitetalk.api.netsuite.com/app/site/hosting/scriptlet.nl?script=<SCRIPT_ID>&deploy=<DEPLOY_ID>&soId=' + record.id />
<#assign suiteletResponse = suiteletURL?content_as_string />
<#assign itemLines = suiteletResponse?eval />
<#list itemLines as line>
<tr>
<td>${line.name}</td>
<td>${line.quantity}</td>
<td>${line.amount}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</pdf>
Replace `<ACCOUNT_ID>` with your NetSuite account ID, `<SCRIPT_ID>` with the Suitelet script ID, and `<DEPLOY_ID>` with the deployment ID of your Suitelet.
Comments
Post a Comment