In this tip i am going to create a button on a record. Clicking the button retrieves the necessary data from the current record and creates a PDF file. The created PDF will then be shown in a seperate window by using a Suitelet.
function PrintPdf(request, response) { try { //retrieve the sales order Id passed to the suitelet. var salesorderId = request.getParameter('id'); var salesOrderRecord = nlapiLoadRecord('salesorder', salesorderId); //Retrieve some values from the sales order record which you want to display in pdf var customer = salesOrderRecord.getFieldText('entity'); var date = salesOrderRecord.getFieldValue('trandate'); // create a table to present the results of the search var strName = "<table>"; strName += "<tr>"; strName += "<td>"; strName += "Customer: "; strName += customer; strName += "</td>"; strName += "</tr>"; strName += "<tr>"; strName += "<td>"; strName += "Date: "; strName += date; strName += "</td>"; strName += "</tr>"; strName += "</table>"; nlapiLogExecution('DEBUG', 'Table created'); // build up BFO-compliant XML using well-formed HTML var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"; xml += "<pdf>\n<body font-size=\"12\">\n<h3>Printing PDF Example</h3><br />\n"; xml += "<p></p>"; xml += strName; xml += "</body>\n</pdf>"; // run the BFO library to convert the xml document to a PDF var file = nlapiXMLToPDF(xml); // set content type, file name, and content-disposition response.setContentType('PDF', 'Print.pdf ', 'inline'); // write response to the client response.write(file.getValue()); } catch (exception) { nlapiLogExecution('DEBUG', 'Error in PrintPdf: ', exception); } }
2. Now create a Client script that will call the suitelet using nlapiResolveURL()where the internal id of the current record is passed to the suitelet. And as you can see in deployment section no function is specified because the fucntion will be called by anUserEvent Script on custom button click.
function PrintBtn() { try { //call the suitelet var createdPdfUrl = nlapiResolveURL('SUITELET', 'customscript_suiteletpdf', 'customdeploy_suiteletpdf', false); //pass the internal id of the current record createdPdfUrl += '&id=' + nlapiGetRecordId(); //show the PDF file newWindow = window.open(createdPdfUrl); } catch (exception) { //alert("Error: " + exception); } }
3. After this create an User Event script that will add a custom button on salesorder record.
function UserEventAddPrintBtn() { try { var internalId = nlapiGetRecordId(); var button = form.addButton('custpage_button', 'Print PDF',"PrintBtn();"); //set the internal id of the created Client script. form.setScript('customscript_generatepdf'); } catch (exception) { nlapiLogExecution('DEBUG', 'Error in AddButton()', exception); } }