Friday, July 12, 2019

Send consolidated transaction PDF as attachment using nlapiXMLToPDF via SuiteScript

Since nlapiPrintRecord currently doesn't support multiple transaction IDs for the ID parameter, you cannot consolidate the actual NetSuite printouts into a single PDF. But using the sample code below, you can generate PDFs the same way as that in Transactions > Management > Print Checks and Forms > Invoices > select multiple Invoices > Print.

 var sResult = new Array();
 
 sResult = nlapiSearchRecord('transaction', 'customsearch', null, null); //load an existing transaction saved search or create a search
 
 var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n";
   xml += "<pdfset>";
 
 for(var i=0; sResult != null && i < sResult.length; i++){
  
  var sColumns = sResult[i].getAllColumns();
  
  //generate invoice printout
  var pdfFile = nlapiPrintRecord('TRANSACTION', sResult[i].getValue(sColumns[0]),'PDF');
  
  //set target folder in file cabinet
  pdfFile.setFolder(13);
  //Set Available without login to true
  pdfFile.setIsOnline(true);
  
  //store file in cabinet
  var fileID = nlapiSubmitFile(pdfFile);
  
  // load the file to get its URL
  var fileURL = nlapiLoadFile(fileID).getURL();.

  var pdf_fileURL = nlapiEscapeXML(fileURL);

    xml += "<pdf src='"+ pdf_fileURL +"'/>";
 }
 
 
   xml += "</pdfset>";
   var consolidatedPDF = nlapiXMLToPDF(xml);
  
   nlapiSendEmail('-5', 'email@netsuite.com', "Subject", 'Body', null, null, null, consolidatedPDF);

No comments:

Post a Comment