4

I want to show the PDF Template in new window while clicking the button in Sales Order. I created the button in sales order process using user event script. after that i'm unable to proceed it. It is possible to show the custom PDF template in new window while clicking the sales order?

My CODE:

USER EVENT SCRIPT:

     // creating button in user event script before load event in view mode
        unction userEventBeforeLoad(type, form, request){  


    if(type == 'view'){
         var internalId = nlapiGetRecordId();

            if (internalId != null) {
                var createPdfUrl = nlapiResolveURL('SUITELET', 'customscript_back0rdered_itm_pdf', 'customdeploy_backord_itm_pdf_dep', false);
                createPdfUrl += '&id=' + internalId;

                //---add a button and call suitelet on click that button and it will open a new window
                var addButton = form.addButton('custpage_printpdf', 'Print PDF', "window.open('" + createPdfUrl + "');");
            }
            else {
                nlapiLogExecution('DEBUG', 'Error', 'Internaal id of the record is null');
            }
    }
}


SUITELET SCRIPT:

function suitelet(request, response){
     var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n";
     xml += "<pdf>";
     xml += "<head><macrolist><macro id=\"myfooter\"><p align=\"center\"><pagenumber /></p></macro></macrolist></head>";
     xml += "<body size= \"A4\" footer=\"myfooter\" footer-height=\"0.5in\">";
     var record = request.getParameter('internalId');
     xml +="record";       //Add values(in string format) what you want to show in pdf
     xml += "</body></pdf>";
     var file = nlapiXMLToPDF(xml);
     response.setContentType('PDF', 'Print.pdf ', 'inline');
     response.write(file.getValue()); 
}

thanks in advance

Deepan Murugan
  • 721
  • 2
  • 19
  • 41
  • In your code you're doing nothing except creating a button and logging the status. Where is your code to read/download ? – Rockstar Jun 27 '16 at 07:36

1 Answers1

8

The way I did it recently:

  • User Event Adds the Button that calls a suitelet (window.open('suitelet URL'))

  • Suitelet Renders the custom template

You can do the rendering like this insise a Suitelet (params: request, response), the custscript_pdf_template points to an html file on the cabinet using the NetSuite Advanced HTML syntax

    var template = nlapiGetContext().getSetting('SCRIPT', 'custscript_pdf_template');
    var purchaseOrder = nlapiLoadRecord('purchaseorder', tranId);
    var xmlTemplate = nlapiLoadFile(template);
    var renderer = nlapiCreateTemplateRenderer();
    var file;

    xmlTemplate = xmlTemplate.getValue();

    renderer.setTemplate(xmlTemplate);
    renderer.addRecord('record', purchaseOrder);

    xmlTemplate = renderer.renderToString();

    file = nlapiXMLToPDF(xmlTemplate);
    resObj = file.getValue();
    response.setContentType('PDF', 'printOut.pdf', 'inline');
    response.write(resObj)
felipechang
  • 916
  • 6
  • 14
  • 1
    HIi pipechang, i did the logic based on your's answer : on clicking the button in user event script it opens the suitelet in new window and trying to render to create custom template using suitelet but i'm get getting how to do that,could you please give an example code for that – Deepan Murugan Jun 27 '16 at 09:35