Last active
July 26, 2024 16:03
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 protected]'; | |
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