Skip to content

Instantly share code, notes, and snippets.

@W3BGUY
Last active September 23, 2022 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save W3BGUY/eb7aa45b027a964f8d2ba96af198339c to your computer and use it in GitHub Desktop.
Save W3BGUY/eb7aa45b027a964f8d2ba96af198339c to your computer and use it in GitHub Desktop.
NetSuite RESTlet Script to accept an invoice internal ID, render the invoice into a PDF, and return a payload that includes the invoice PDF contents.
/**
* @NApiVersion 2.1
* @NScriptType Restlet
* @NModuleScope SameAccount
* @Author Charles.Bastian (Technocrat Consulting Inc)
* @Created 2022-00-00
* @ScriptName TCI_RS_Create_Invoice_PDF
* @Filename TCI_RS_Create_Invoice_PDF.js
* @ScriptID customscript_tci_rs_create_invoice_pdf
* @SandBox
* @FileID
* @InternalURL
* @DeployURL
* @SearchURL N/A
* @FolderURL N/A
* @Production
* @FileID
* @InternalURL
* @DeployURL
* @SearchURL N/A
* @FolderURL N/A
*
* @modifications
* Date Author Version Remarks
* 2022-09-23 Charles.Bastian v22.9.23-1 Created for Jitterbit client SPL
*
*/
define(['N/record','N/render','N/runtime','N/email'],function(record,render,runtime,email){
function doGet(requestParams) {
let nsID=0;
let jsonResponsePayload={
"error":"",
"nsID":0,
"pdfString":""
};
try{
if(requestParams.custparamNSID!==undefined){
log.debug('in doGet',JSON.stringify(requestParams.custparamNSID));
nsID=requestParams.custparamNSID;
if(nsID.length>0){
var pdfString='';
var pdfInvPrint=render.transaction({entityId:parseInt(nsID),printMode:render.PrintMode.PDF});
var pdfContent=pdfInvPrint.getContents();
pdfString=pdfContent;
log.debug('buildPDF.pdfString',JSON.stringify(pdfString));
jsonResponsePayload={
"error":"",
"nsID":nsID,
"pdfString":pdfString
};
}else{
jsonResponsePayload={
"error":"No invoice internal ID value received.",
"nsID":nsID,
"pdfString":""
};
}
}else{
jsonResponsePayload={
"error":"No invoice internal ID parameter (custparamNSID) received.",
"nsID":nsID,
"pdfString":""
};
}
}catch(err01){
log.error('doGet.err01',err01);
jsonResponsePayload={
"error":err01.message,
"nsID":nsID,
"pdfString":""
};
handleErrorAndSendNotification(err01,'doGet');
}finally{
return JSON.stringify(jsonResponsePayload);
}
}
/***************************/
/***** ERROR FUNCTIONS *****/
/***************************/
function handleErrorAndSendNotification(e,stage){
log.error('Stage: '+stage+' failed',e);
var author=-5;
var recipients='email@domain.com';
var subject='Map/Reduce script '+runtime.getCurrentScript().id+' failed for stage: '+stage;
var body='An error occurred with the following information:\n'+'Error code: '+e.name+'\n'+'Error msg: '+e.message;
try{
email.send({
author:author,
recipients:recipients,
subject:subject,
body:body
});
}catch(err01){
log.error('handleErrorAndSendNotification.err01',JSON.stringify(err01));
}
}
/*******************************/
/***** END ERROR FUNCTIONS *****/
/*******************************/
return{
'get':doGet
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment