Entartes Blog

Effortlessly Rename NetSuite PDFs | Entartes

Written by Maher Chamma | Sep 5, 2022 6:58:26 PM

When NetSuite generates a transaction PDF file, the file name by default takes the id of the record rather than the transaction number. This is a major annoyance to many users and many are often so used to it that renaming the file is just part of their habit. Even though it may not take very long, the few seconds can add up over thousands of transactions.

Use the method and scripts below to work around this limitation on any transaction document in a few minutes:

 

  1. Add the suitelet below to render the PDF with your own file naming convention:

    /**
     * @NApiVersion 2.1
     * @NScriptType Suitelet
     * @since 9/5/2022
     * @author Maher Chamma
     * @description
     */

    define(['N/render', 'N/search'],
      function(render, search) {
        function onRequest(context) {
          const req = context.request;
          const params = req.parameters;
          if (req.method === 'GET') {
            const tranLup = search.lookupFields({ type: 'transaction', id: params.id, columns: ['tranid', 'type'] });
            let pdfFile = null;
            if (tranLup.type[0].value === 'WorkOrd') {
              pdfFile = render.bom({ entityId: Number(params.id) });
            } else {
              pdfFile = render.transaction({ entityId: Number(params.id) });
            }
            pdfFile.name = tranLup.tranid + '.pdf';
            return context.response.writeFile({ file: pdfFile });
          }
        }
        return {
          onRequest: onRequest
        };
      }
    );

    In the case above we just used the transaction number itself so it will output as SO1786.pdf for example. If you want to customize the file name further you can pass more columns into the lookup such as the transaction date or customer.
  2. Add the user event code to render the custom print button on your transaction record:

    /**
    *@NApiVersion 2.1
    *@NScriptType UserEventScript
    */
    define(['N/url'], function(url) {
      function beforeLoad(context) {
        if (context.type === context.UserEventType.VIEW) {
          const newRec = context.newRecord;
          try {
            const uiForm = context.form;
            const slUrl = url.resolveScript({
              scriptId: 'customscript_ea_alt_print_sl',
              deploymentId: 'customdeploy1',
              params: {
                id: newRec.id
              }
            });
            uiForm.addButton({ id: 'custpage_ea_alt_print', label: 'Print', functionName: 'window.open("' + slUrl + '","_self")' });
          } catch (err) {
            log.error('err:' + newRec.id, err);
          }
        }
      }
      return {
        beforeLoad: beforeLoad
      };
    });

In step # 2 the url.resolveScript is generating the URL to your suitelet in step 1 so be careful to lineup the script id and deployment id of the suitelet script record to match.

If you only want to have a single print option per record make sure to also remove the native Print option by customizing your form.

Hope this helps and you get back precious minutes of the day!