Button click display/download PDF as response using SuiteLet

In some case in transaction record the client may request to download the invoice in multiple format templates according to the customer category. We can create multiple format templates of invoices. But at default we can set one template.

In this case we can create a button on the sales order record while clicking on that button it will redirect to suitelet and the desired PDF template is displayed/downloaded.

USER EVENT:

let form = scriptContext.form;

    let recordId = scriptContext.newRecord.id;

    let customTemplate = ‘/app/site/hosting/scriptlet.nl?script=1464&deploy=1&recordId=’ + recordId;  //Suitelet File

    form.addButton({

      id: ‘custpage_custom_button’,

      label: ‘Custom Button’,

      functionName: `window.open(‘${customTemplate}‘,’_blank’)`

    })

SUITELET:

const onRequest = (scriptContext) => {

    try {

      // Get the record ID from the request parameter

      let recordId = scriptContext.request.parameters.recordId;

      log.debug(“Record ID Received”, recordId);

      let renderer = render.create();

      renderer.addRecord({

        templateName: ‘record’,

        record: record.load({

          type: record.Type.SALES_ORDER,

          id: recordId

        }),

      });

      renderer.setTemplateById(100); //Specified PDF template ID

      let pdfFile = renderer.renderAsPdf();

      scriptContext.response.writeFile(pdfFile, true);

    } catch (e) {

      log.error(“Error in onRequest”, e);

    }

  };

Leave a comment

Your email address will not be published. Required fields are marked *