Requirement
We need to download an invoice pdf from the listed invoices when clicking on the link provided against each line of invoices in suitelet page.
Solution
We can provide a suitelet link as download link. While clicking on the link, that particular invoice is downloaded in pdf format.
This can be done by using a client script and a suitelet script.
Following is the code snippet for this action. This following function is called in the download link in suitelet page.
Client script.js
function downloadInvoice(invId){
try {
var createLinkUrl = url.resolveScript({
scriptId: 'scriptId',
deploymentId: 'deploymentId',
returnExternalUrl: true,
params: {
custscriptinvid: invoiceId,
}
});
window.open(createLinkUrl);
}
Suitelet Script.js
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/currentRecord', 'N/ui/serverWidget','N/render','N/search'],
/**
* @param{currentRecord} currentRecord
* @param{serverWidget} serverWidget
* * @param{render} render
* * @param{search} search
*/
(currentRecord, serverWidget,render,search) => {
/**
* 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 parms = scriptContext.request.parameters.custscriptinvid;
var downloadPdf = render.transaction({
entityId: parseInt(parms),
printMode:render.PrintMode.PDF,
inCustLocale: true
})
var docNum = search.lookupFields({
type:"transaction",
id: parseInt(parms),
columns: ["tranid"]
}).tranid;
var today=new Date();
downloadPdf.name = docNum +'_' +today+ ".pdf";
scriptContext.response.writeFile(downloadPdf, false)
}
catch (e) {
log.debug('Error@On request',e)
}
}
return {onRequest}
});