![NetSuite ERP](https://inatechnologiespk.com/wp-content/uploads/2023/06/31julySM1-1024x536.jpg)
NetSuite – Custom Advance PDF HTML Report With Custom Button via SuiteScript and Suitelet.
To enhance your NetSuite experience, creating a custom PDF template with an associated button is a powerful tool. This article guides you through the process using three SuiteScripts: a User Event Script, a Client Script, and Suitelet.
User Event Script:
/**
* @NScriptType UserEventScript
* @NApiVersion 2.1
*/
define([‘N/record’], function (record) {
function addButton(context) {
var rec = context.newRecord;
log.debug(“Current Rec”,rec);
var form = context.form;
form.addButton({
id: “custpage_print”,
label: “Print Customer Deposit”,
functionName: “redirectToPrint”
});
form.clientScriptFileId = ‘please enter your client script id’
log.debug(“ClientScript called”);
}
return {
beforeLoad: addButton
}});
Next, create the Client Script and associate it with the above User Event Script.
Client Script:
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define([“N/currentRecord”,”N/url”], function(currRecord, url) {
function redirectToPrint(){
try{
log.debug(‘Custom Button’, ‘Button Clicked’);
var record = currRecord.get();
var id = record.id;
log.debug(‘Client Script ‘,id);
var suiteletURL = url.resolveScript({
scriptId:’customscript_customer_desposite_sl’,
deploymentId:’customdeploy_customer_desposite_sl’,
returnExternalUrl:false,
params : {
‘cid’ :id
}
});
window.open(suiteletURL);
}
catch(e){
log.debug({
title : ‘Error’,
message : e
});
}
}
function pageInit(){}
return {
pageInit: pageInit,
redirectToPrint: redirectToPrint
}
});
Now, let’s create the Suitelet for PDF customization.
Suitelet:
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define([‘N/xml’, “N/record”, “N/render”, “N/file”, “N/search”, “N/format”],
(xml, record, render, file, search, format) => {
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request – Incoming request
* @param {ServerResponse} scriptContext.response – Suitelet response
* @since 2015.2
*/
const onRequest = (scriptContext) => {
try {
var id = scriptContext.request.parameters[“cid”];
log.debug(‘On Request’, ‘Id is ‘ + id);
var objRecord = record.load({
type: “customerdeposit”,
id: id,
isDynamic: false,
});
// you can add any field what ever you want as per you business requirement need
var saleID = objRecord.getValue({fieldId: ‘salesorder’});
var tranid = objRecord.getValue({fieldId: ‘tranid’});
var trandate = objRecord.getValue({fieldId: “trandate”});
var transactionfees = objRecord.getValue({fieldId: “custbody_ina_transaction_fees”});
var payment = objRecord.getValue({fieldId: “payment”});
var transactionfeesv;
log.debug(“transactionfees”,transactionfees);
if (transactionfees==0 || !transactionfees)
{
transactionfeesv=0.00;
}
else{
transactionfeesv=transactionfees;
}
log.debug(“transactionfeesv”,transactionfeesv);
// Sales Order Body Fields
var osRecord = record.load({
type: “salesorder”,
id: saleID,
isDynamic: false,
});
var billaddress = osRecord.getText({fieldId: “billaddress”});
var shipaddress = osRecord.getText({fieldId: “shipaddress”});
var saletranno = osRecord.getValue({fieldId: “tranid”});
var terms = osRecord.getText({fieldId: “terms”});
var shipmethod = osRecord.getText({fieldId: “shipmethod”});
var subtotal = osRecord.getValue({fieldId: “subtotal”});
var taxtotal = osRecord.getValue({fieldId: “taxtotal”});
var total = osRecord.getValue({fieldId: “total”});
var shippingcost = osRecord.getValue({fieldId: “shippingcost”});
var projectName = osRecord.getValue({fieldId: “custbody2”}); // please add your own custom field as per the requirement
var PO = osRecord.getValue({fieldId: “custbody99”}); // please add your own custom field as per the requirement
// items get function
var items = getItemList(osRecord);
log.debug({
title: “data source”,
details: JSON.stringify(items)
});
// date format get function
const getDate = dateFormat(trandate)
var setZero = 0;
var pageRenderer = render.create();
var dataSource = {
trandate: getDate,
tranid: tranid,
shipmethod: shipmethod,
saletranno: saletranno,
shipaddress : shipaddress,
billaddress : billaddress,
terms : terms,
payment : payment.toFixed(2) || setZero.toFixed(2),
transactionfeesv : transactionfeesv.toFixed(2) || setZero.toFixed(2),
total : total.toFixed(2) || setZero.toFixed(2),
taxtotal : taxtotal.toFixed(2) || setZero.toFixed(2),
subtotal : subtotal.toFixed(2) || setZero.toFixed(2),
shippingcost : shippingcost.toFixed(2) || setZero.toFixed(2),
projectName : projectName,
PO : PO,
items: items
};
log.debug({
title: “data source”,
details: JSON.stringify(dataSource)
});
var templateFile = file.load({
id: 10425
});
pageRenderer.templateContent = templateFile.getContents();
pageRenderer.addCustomDataSource({
format: render.DataSource.OBJECT,
alias: ‘data’,
data: dataSource,
})
var renderedPage = pageRenderer.renderAsString();
scriptContext.response.renderPdf(renderedPage)
} catch (e) {
log.debug(“Error”,e);
}
}
function dateFormat(tranDate) {
const date = new Date(tranDate);
const options = {day: ‘numeric’, month: ‘numeric’, year: ‘numeric’};
const dateString = date.toLocaleDateString(options);
const splitDate = dateString.split(‘-‘);
const year = splitDate[0];
const month = parseInt(splitDate[1]);
const day = parseInt(splitDate[2]);
const formattedDate = ${month}/${day}/${year};
return formattedDate;
}
function getItemList(objRecord)
{
var items = [];
var lineCount = objRecord.getLineCount({
sublistId: ‘item’
});
for (var i = 0; i < lineCount; i++) {
var itemId = objRecord.getSublistValue({
sublistId: ‘item’,
fieldId: ‘item’,
line: i
});
var itemName = search.lookupFields({
type: search.Type.ITEM,
id: itemId,
columns: [‘itemid’]
});
var amount = objRecord.getSublistValue({
sublistId: ‘item’,
fieldId: ‘rate’,
line: i
}).toFixed(2);
var qty = objRecord.getSublistValue({
sublistId: ‘item’,
fieldId: ‘quantity’,
line: i
});
var total = qty * amount;
total = total.toFixed(2);
var item = {
itemName: itemName.itemid,
description: objRecord.getSublistText({
sublistId: ‘item’,
fieldId: ‘description’,
line: i
}),
quantity: qty,
rate : amount,
totalamount : total
};
items.push(item);
}
return items;
}
return {onRequest}
});
Deploy all the scripts to the desired module and execute them from the transaction record.
Thank you.