How to add a script in NetSuite to generate a Purchase Order PDF after the final approval

How to add a script in NetSuite to generate a Purchase Order PDF after the final approval

Overview

In NetSuite, there are no native APIs that allow ApprovalMax to generate Purchase Order (PO) PDF files automatically. However, you can add a custom PO PDF generation SuiteScript to achieve this functionality.
The process involves five key steps:
  1. Download the PO PDF Template: obtain the PO PDF template from the desired source. This template will serve as the basis for generating the PO PDFs in NetSuite.
  2. Organise File Cabinet Folders: set up the necessary file cabinet folders within NetSuite to store the PO PDFs.
  3. Prepare the PO PDF Generation Script: utilise a custom script suggested below in NetSuite using SuiteScript, the NetSuite scripting language. This script will define the logic and functionality for generating the PO PDF files.
  4. Create a Script Record in NetSuite: configure a script record in NetSuite to associate the custom script with the appropriate record types and events. This record will enable the script to run automatically when triggered by specific actions or conditions.
  5. Assign necessary permissions to script: add an ApprovalMax role to the script deployment in order to let it execute.
  6. Test PO PDF File Generation After Approval: once the script has been set up, test the PO PDF file generation functionality by initiating the approval process for Purchase Orders. Verify that the script is correctly generating the PO PDF filess and storing them in the designated file cabinet folders.

Download the PO PDF template

To locate the Purchase Order Template that should be used for PDF generation, follow these steps in NetSuite:
1. Go to Customization.
2. Select Forms.
3. Choose Advanced PDF/HTML Templates.
4. Look for the Purchase Order Template that you want to use for PDF generation.



5. Once you've located the desired Purchase Order Template, click on Actions.
6. From the dropdown menu, select Download Template.


Organise File Cabinet folders

To organise the File Cabinet folders for managing the PDF template and generated PO PDF files in NetSuite, you can follow these steps:
1. Create a folder in the File Cabinet: Navigate to the File Cabinet section in NetSuite and create a new folder specifically for your PO-related files. This folder will serve as the main location for storing the PDF template and generated PO PDF files.
2. Upload the PDF template: Once the folder has been created, upload the PDF template file into this folder. This template will be used as the basis for generating the PO PDFs.
3. Create a folder for storing the generated PO PDF files: Next, create another folder within the File Cabinet to store the generated PO PDF files (or use the same folder as above).



Prepare the PO PDF generation script

To prepare the PO PDF generation script, you need to update the provided script as follows:
  1. Path to PO PDF template on line 15
  2. Internal Id of Folder where generated PO PDF files will be stored on line 17
Save the file as a Javascript file with .js extension.
  1. /**
  2.  *@NApiVersion 2.x
  3.  *@NScriptType UserEventScript
  4.  */


  5. define (['N/render', 'N/file', 'N/record'],
  6.     function(render, file, record) {
  7.         function generatePoPdf(context) {
  8.             try {
  9.                 if (context.type !== context.UserEventType.CREATE) return;

  10.                 // fill in file cabinet path to PO PDF template
  11.                 var poPdfTemplatePath = 'Templates/PDF Templates/custtmpl_100_1234567_790.template.xml';
  12.                 // fill in file cabinet folder id where generated PO PDFs will be stored
  13.                 var poPdfFolderId = 1234;

  14.                 var poRecord = record.load({
  15.                   type: record.Type.PURCHASE_ORDER,
  16.                   id: context.newRecord.id,
  17.                 });

  18.                 // to run only when approved PO is created by ApprovalMax
  19.                 var newApprovalStatus = poRecord.getValue({fieldId: 'approvalstatus'});
  20.                 if(newApprovalStatus !== '2') return;

  21.                 var poSubsidiaryId = poRecord.getValue({ fieldId: "subsidiary" });
  22.                 var xmlTemplateFile = file.load(poPdfTemplatePath);

  23.                 var renderer = render.create();
  24.                 renderer.templateContent = xmlTemplateFile.getContents();
  25.                 renderer.addRecord('record', poRecord);

  26.                 // you need to explicitly load all reference objects which properties are used on the PDF template
  27.                 renderer.addRecord({
  28.                     templateName: "subsidiary",
  29.                     record: record.load({
  30.                       type: "subsidiary",
  31.                       id: poSubsidiaryId,
  32.                     })
  33.                 });

  34.                 var poPdf = renderer.renderAsPdf();

  35.                 var poNumber = poRecord.getValue({ fieldId: "tranid" });

  36.                 var poSubsidiary = record.load({
  37.                     type: record.Type.SUBSIDIARY,
  38.                     id: poSubsidiaryId,
  39.                 });
  40.                 var poSubsidiaryName = poSubsidiary.getValue({ fieldId: "name" });

  41.                 poPdf.name = "PO-" + poNumber + " from " + poSubsidiaryName + ".pdf";
  42.                 poPdf.folder = poPdfFolderId;
  43.                 var fileId = poPdf.save();

  44.                 record.attach({
  45.                     record: {
  46.                         type: 'file',
  47.                         id: fileId
  48.                     },
  49.                     to: {
  50.                         type: 'purchaseorder',
  51.                         id: poRecord.id
  52.                     }
  53.                 });

  54.                 log.audit({
  55.                     title: 'PO PDF generated',
  56.                     details: 'PO ID: ' + poRecord.id + '. PO Number: ' + poNumber + '. FileId: ' + fileId
  57.                 });

  58.             } catch (e) {
  59.                 log.error ({
  60.                     title: e.name,
  61.                     details: e.message
  62.                 });
  63.             }
  64.         }

  65.         return {
  66.             afterSubmit: generatePoPdf
  67.         };
  68.     });

Create a script record in NetSuite

To create a script record in NetSuite, follow these steps:

1. Go to Customization.
2. Select Scripting.
3. Choose Scripts.
4. Click on New to create a new script.
5. Upload your script file by clicking on the Upload Script File button and selecting the JavaScript file you prepared.
6. Once the script file has been uploaded, click on Create Script Record.





After clicking on Create Script Record, you will be redirected to the script creation page. Here, you need to fill in the following fields:
  1. Name: Enter any name you'd like for the script.
  2. On the Deployments subtab:
    1. Applies to: Select Purchase Order to specify that the script is applied to Purchase Order records.
    2. Deployed: Choose Yes to indicate that the script is deployed and active.
    3. Status: Set the status to Released.
    4. Event Type: Select Create to determine that the script is triggered when a new Purchase Order record is created.
    5. Log Level: Choose Audit to enable auditing of the script's execution for debugging purposes.
  3. Click on Save to create the script record.

Assign necessary permissions to the script

After the script record has been saved, open the Deployments tab and click on the Purchase Order link in "Applies To" column:


After clicking on Applies To, you will be redirected to the Script Deployment record. Here, you need to do the following:
  1. On the Audience subtab:
    1. Select the ApprovalMax role in the Roles field in the Audience tab
  2. Click on Save to create the Script Deployment record.

Test PO PDF file generation after approval

To test the PO PDF file generation after approval, you can follow these steps based on the method of creating the Approved Purchase Order:

If you create a Purchase Order directly in NetSuite:
1. Create an Approved Purchase Order in NetSuite. You can do this by navigating to the Purchase Order module and creating a new PO record. Make sure to set the status of the PO as Approved before saving it. You can also select the desired PDF template for the PO.
2. After the approval, a PDF file with a name such as "PO-1760 from Subsidiary B.pdf" will be generated based on the selected PDF template. The generated PDF file will be stored in the appropriate location within NetSuite's File Cabinet.




If you create a PO in ApprovalMax:
1. Create a Purchase Order in ApprovalMax and submit it for approval.
2. After the final approval, the generated PO PDF file will be downloaded from NetSuite to ApprovalMax.
3. An email will be sent to the Supplier automatically, indicating that the Purchase Order has been sent. However, please note the following conditions:
  1. The email to the Supplier will not be sent if ApprovalMax encounters any issues with downloading the PO PDF file from NetSuite.
  2. ApprovalMax will attempt to download the PO PDF file three times, with an interval of 30 seconds, after the final approval.
  3. If there are any invalid email addresses in the "To" or "Cc" sections of the email, it will not be sent.

4. In the audit trail of the Purchase Order in ApprovalMax, you will see an event labelled "Purchase Order has been sent to Supplier," indicating that the email was sent successfully.